Allow us to provide an alternate name
Allow us to provide alternate names for input elements using DataAnotation for Model-Properties. Sample:
[AlternateNames(true, "un", "cn")]
public int UserName {get;set;}
So the DefaultModelBinder respects this attribute and searches for "UserName", if not found searches for "un" and finally for "cn".
Helpers like TextBoxFor<> should also consider this attribute. When the first boolean argument is true (force alternate name) the HtmlHelper should choose the first alternate name as the input name ("un") otherwise choose the property name.
This attribute would allow to use models for forms that are posting to pages that are using other technologies or minimize parameter names but keeping meaningful property names.
We are currently migrating from asp.net webforms and a custom presentation layer to mvc and do have to reimplement a lot of stuff in mvc 3 to keep compatibility between the technologies.
Thanks!
Jens
6 comments
-
jrummell
commented
A better solution might be found using ViewModels (http://lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models/).
-
Jens Hofmann
commented
@Jeffrey This does only solve half the problem. The most important part is to get the Helper-Methods (TextBoxFor.. and so on) to respect it during html name-attribute binding. And there is no existing hook usable to implement it. We implemented wrappers around the standard Helpers to solve the problem.
-
Jeffrey Palermo
commented
Ok. I got parameter attributes working. Now the API is
public ActionResult([Alias("pid")] Product product){...}Alias also works on properties of complex model-bound objects.\
http://nuget.org/packages/ActionParameterAlias -
Jeffrey Palermo
commented
Ok. I got parameter attributes working. Now the API is
public ActionResult([Alias("pid")] Product product){...}Alias also works on properties of complex model-bound objects.\
http://nuget.org/packages/ActionParameterAlias -
Jeffrey Palermo
commented
I took this and ran with it. Download my Nuget package that allows the adding of action parameter aliases. http://nuget.org/packages/ActionParameterAlias
I don't have parameter attributes working like the sample above, but that shouldn't be too hard. It's a start.
Regards,
Jeffrey Palermo -
Peter
commented
The number of times I could have used this...