Wednesday, 15 August 2012

asp.net mvc - Change Name of Html.DropDownListFor - MVC Razor -



asp.net mvc - Change Name of Html.DropDownListFor - MVC Razor -

i creating form drop downwards property named "event". unfortunately reserved word won't work usual:

@html.dropdownlistfor(x => x.event, new selectlist(viewdata["events"] ienumerable<jfs.data.model.event>, "id", "name"))

in controller can't take value separately reserved word, if this:

public actionresult create(int event) { etc etc }

it throws error.

what ideally alter name of dropdown list, this:

@html.dropdownlistfor(x => x.event eventid, new selectlist(viewdata["events"] ienumerable<jfs.data.model.event>, "id", "name"))

but doesn't work. know of right way alter name? :-)

you cannot alter name generated html helpers , design. instead alter name of action parameter , prefix @ allows utilize reserved words in c# variable names:

public actionresult create(int @event) { etc etc }

but in general not recommended utilize reserved words variable names unless absolutely necessary. , in case not absolutely necessary because there's much improve solution of course of study consists in using view model:

public class createeventviewmodel { public int event { get; set; } }

and having controller action take view model argument:

public actionresult create(createeventviewmodel model) { etc etc }

asp.net-mvc razor

No comments:

Post a Comment