c# - Change name the For helper methods create -


i making form mvc , using [controltype]for([expression]) helper methods (eg html.textboxfor(...))

to bind data controls using following viewmodel:

public class userviewmodel {     //the model used bind data elements     public usermodel user { get; set; }      //used bind selectable options dropdownlists     public selectlist descenttypes { get; set; }     public selectlist gendertypes { get; set; } } 

when using name of controls set name="property.subproperty" (eg name="user.id") rather show name="id" on html form.

is possible without having write lot of custom code framework can translate viewmodel (userviewmodel) or model (user) itself?

i'd advise leaving default naming in place unless have reason alter it. ids (which appears question leaning towards) more flexible.

changing ids

ids aren't submitted form, can set them desired without breaking model binding. default, hierarchical, can override them inline:

@html.textboxfor( o => o.username, new { id = "foo" } ) 

of course, manual work.

if big concern external js/css, i'd suggest using class names , data-* attributes in (css/jquery/whatever) selectors rather ids.

@html.textboxfor( o => o.user.username, new { data_role="grand-total" } ) 

it's still manual, it's descriptive , independent of id.

sometimes use snippet of script in views initialize larger js class data available directly within view. lets bulk of script reside in external file while allowing dynamic values used initialize it. useful more ids.

altering generated markup , binding

for reference, let's wanted change id and name.

  1. write own htmlhelper extension methods create markup want. wrap existing methods not take expression , pass explicit values them indicate name want.
  2. write own modelbinder map raw form collection.
  3. determine strategy dealing hierarchical objects (which main reason naming convention exists in first place).

item #3 addressed decorating properties indicate how naming should performed , how model binding should map. become complicated quickly.

public class userviewmodel {     // use metadata determine how handle properties on member     [flatten]     public usermodel user { get; set; }      public selectlist descenttypes { get; set; }     public selectlist gendertypes { get; set; } } 

alternatives

  1. flatten view model adding user's properties directly it. looks composing view model domain model(s). isn't idea. i'd suggest reading pros/cons of binding directly domain models.

  2. leave naming alone. isn't hurting , makes life easy. can avoid ever directly working names/ids in client code using helper methods.

for example:

// javascript + razor var name = "@html.namefor( o => o.user.id )"; alert(name); 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -