In my mvc4 application, I have drop down list set up on a page Register like this
In AccountModel.cs's HttpGet Register method, I initialize its data with this code
public ActionResult Register()
{
MyModel mm=new MyModel();
mm.mydropdown=new []
{
new SelectListItem{Value="value", Text="text"}
////...
};
}
Only when this page loads, will that code get run; but is there anywhere I can insert the code snippet that it must be compiled and have my mydropdown
always initialized in the application such that I don't have to rewrite it in [HttpPost]
method ?
[update]
Here is the mymodel
class
public class Register
{
[Required(ErrorMessage="Option is required")]
public string option;
public IEnumerable<SelectListItem> Options;
}
In my case I use option
in cshtml like this
@Html.DropDownListFor(m=>m.option, Model.Options,"Select an item")
In HttpPost
Register method, I must reinitialize the Options or I will run into error "Option is of type string but it must be IEnumerable<SelectListItem> instead
" otherwise