asp.net mvc - Return model from Partial View issue -
i have created partial view (even though editor template), pass sub model view, however, when clicked "submit", "null" partial view. can main model's properties values except sub model one.
main model
public class petmodel { public string name {get; set;} public long speciesid {get; set;} public long breedid {get; set;} public calendar dob {get; set;} }
sub model
public class calendar { public string year{get; set;} public string month{get; set;} public string day{get; set;} }
main view
@model application.models.petmodel @using (html.beginform("catchpetcontent", "quote",model)) { @html.textboxfor(x => x.name) @html.dropdownlistfor(x=>x.speciesid,new list<selectlistitem>(),"select") @html.dropdownlistfor(x=>x.breedid,new list<selectlistitem>(),"select") @html.editorfor(model => x.dob) <input type="submit" value="submit" /> }
editor template
@model application.models.calendar @html.dropdownlistfor(model => model.day, new list<selectlistitem>()) @html.dropdownlistfor(model => model.month,new list<selectlistitem>()) @html.dropdownlistfor(model => model.year, new list<selectlistitem>())
"catchpetcontent" action
[httppost] public actionresult catchpetcontent(petmodel model) { petmodel pet = new petmodel(); pet.name = model.name; pet.speciesid = model.speciesid; pet.breedid = model.breedid; pet.dob = model.dob;// null routevaluedictionary redirecttargetdictionary = new routevaluedictionary(); redirecttargetdictionary.add("controller", "home"); redirecttargetdictionary.add("action", "index"); return new redirecttorouteresult(new routevaluedictionary(redirecttargetdictionary)); }
when debugged it, "model.dob" null
you should add sub-property parameter on action:
[httppost] public actionresult catchpetcontent(petmodel model, calendar bob) { ** snip ** }
the default modelbinder doesn't nest objects. find values if include second parameter.
if want nest them, you'd have create own modelbinder.
the following question had similar issue: list count empty when passing view model in asp.net mvc
Comments
Post a Comment