asp.net mvc - submit a list become null in the post + MVC -
i have viewmodel:
public class registrationviewmodel { public string country { get; set; } public configurationparamvalue countryparam { get; set; } public string civility { get; set; } public configurationparamvalue civilityparam { get; set; } [firstnamevalidator(category = "registration", islocal = false )] public string firstname { get; set; } public configurationparamvalue firstnameparam { get; set; } [lastnamevalidator(category = "registration", islocal = false)] public string lastname { get; set; } public list<int> days { get; set; } public int selectedday{ get; set; } public list<month> months { get; set; } public month selectedmonth { get; set; } public list<int> years { get; set; } public int selectedyear { get; set; } public datetime birthdate { get; set; } } i create view viewmodel :
@model registration.front.web.models.registrationviewmodel @{ viewbag.title = "index"; layout = "~/views/shared/_layout.cshtml"; } <h2>index</h2> @using (html.beginform()) { @html.antiforgerytoken() @html.validationsummary(true) <fieldset> <legend>registrationviewmodel</legend> <div class="editor-label"> @html.labelfor(model => model.country) </div> <div class="editor-field"> @html.editorfor(model => model.country) @html.validationmessagefor(model => model.country) </div> <div class="editor-label"> @html.labelfor(model => model.civility) </div> <div class="editor-field"> @html.editorfor(model => model.civility) @html.validationmessagefor(model => model.civility) </div> <div class="editor-label"> @html.labelfor(model => model.firstname) </div> <div class="editor-field"> @html.editorfor(model => model.firstname) @html.validationmessagefor(model => model.firstname) </div> <div class="editor-label"> @html.labelfor(model => model.lastname) </div> <div class="editor-field"> @html.editorfor(model => model.lastname) @html.validationmessagefor(model => model.lastname) </div> <div class="editor-label"> @html.labelfor(model => model.birthdate) </div> <div class="editor-field"> @html.dropdownlistfor(model => model.selectedday, new selectlist(model.days)) @html.validationmessagefor(model => model.birthdate) </div> <div class="editor-label"> @html.labelfor(model => model.occupation) </div> <div class="editor-field"> @html.editorfor(model => model.occupation) @html.validationmessagefor(model => model.occupation) </div> <div class="editor-label"> @html.labelfor(model => model.zipcode) </div> <div class="editor-field"> @html.editorfor(model => model.zipcode) @html.validationmessagefor(model => model.zipcode) </div> <div class="editor-label"> @html.labelfor(model => model.email) </div> <div class="editor-field"> @html.editorfor(model => model.email) @html.validationmessagefor(model => model.email) </div> <div class="editor-label"> @html.labelfor(model => model.password) </div> <div class="editor-field"> @html.editorfor(model => model.password) @html.validationmessagefor(model => model.password) </div> <div class="editor-label"> @html.labelfor(model => model.cgv) </div> <div class="editor-field"> @html.editorfor(model => model.cgv) @html.validationmessagefor(model => model.cgv) </div> <div class="editor-label"> @html.labelfor(model => model.optin) </div> <div class="editor-field"> @html.editorfor(model => model.optin) @html.validationmessagefor(model => model.optin) </div> <div class="editor-label"> @html.labelfor(model => model.cnil) </div> <div class="editor-field"> @html.editorfor(model => model.cnil) @html.validationmessagefor(model => model.cnil) </div> <p> <input type="submit" value="create" /> </p> </fieldset> } <div> @html.actionlink("back list", "index") </div> this contoller:
public actionresult index() { list<int> listdays = new list<int>(){1, 2, 3}; return view(new registrationviewmodel() { days=listdays }); } [httppost] public actionresult index( registrationviewmodel rvm) { if (modelstate.isvalid) { return redirecttoaction("welcome"); } return view(rvm); } public actionresult welcome() { return view(); } my problem in post, property days of viewmodel null!!!!! how can correct this?
in view not rendering days inside form
render hidden fields inside form name="days" like
foreach(var day in @model.days) { <input type="hidden" name="days" /> }
copy above code , paste after <div class="editor-field"> @html.editorfor(model => model.cnil) @html.validationmessagefor(model => model.cnil) </div>
now,when submit, days values submitted post method , values in days list.
Comments
Post a Comment