asp.net mvc - Updated partial view model values in contoller after form submit -
in sample mvc application have model
class samplemodel { public int id { get; set; } public string name { get; set; } public list<certification> certifications { get; set; } } class certification { public int id { get; set; } public string certificationname { get; set; } public int durationinmonths { get; set; } }
my view (i need certification details shown in partial view)
@model sampleapplication.model.samplemodel <!-- other code... --> @using (html.beginform("savevalues","sample", formmethod.post, new { id= "saveform" })) { @html.hiddenfor(m => m.id, new { id = "hdnid" }) @html.textboxfor(m => m.name, new { id = "txtname" }) @{html.renderpartial("_certdetails.cshtml", model.certifications);} <input type="submit" id="btnsubmit" name="btnsubmit" value="update" /> }
partial view
@model list<sampleapplication.model.certification> <!-- other code... --> @if (@model != null) { (int = 0; < @model.count; i++) { @html.hiddenfor(m => m[i].id , new { id = "cid" + i.tostring() }) @html.textboxfor(m => m[i].certificationname,new{ id ="cname" + i.tostring() }) @html.textboxfor(m => m[i].durationinmonths,new{ id ="cdur" + i.tostring() }) } }
controller
[httppost] public actionresult savevalues(samplemodel sm) { //here not getting updated certification details (in sm) }
how updated values of partial view in controller after form post? able updated certification values when not using partialview. right way or should follow other methods?
if sm.certifications
coming null, means either nothing posted that, or modelbinder unable attach posted data properly.
in partial, you're defining fields indexer, initially, certifications
null list, code never run. means, elsewhere have javascript logic adding new certification
fields page, dynamically, , guess field names javascript generating do not follow indexing convention modelbinder expects. fields should in format of:
listproperty[index].propertyname
so in case, js should generating names like:
certifications[0].certificationname
in order data bound properly.
Comments
Post a Comment