asp.net mvc - MVC Model not updating -
the model
class address { public string street { get; set; } public string city { get; set; } public string state { get; set; } public string zip { get; set; } }
the controller action
[httppost] public actionresult getaddress(address model) { if (!string.isnullorempty(model.zip)) { model.city = getcitybyzip(model.zip); } return view(model); }
the view
<div class="formrow"> @html.labelfor(model => model.city) @html.textboxfor(model => model.city) @html.validationmessagefor(model => model.city) </div> <div class="formrow"> @html.labelfor(model => model.state) @html.dropdownlistfor(model => model.state, (ienumerable<selectlistitem>)viewbag.states, new { style = "width:217px;" }) @html.validationmessagefor(model => model.state) </div> <div class="formrow"> @html.labelfor(model => model.zip) @html.textboxfor(model => model.zip) @html.validationmessagefor(model => model.zip) </div>
the problem whenever city being modified, never gets reflected on view. during debugging, model.city
contains correct value doesn't show on view. simple @html.textboxfor(model => model.city)
doesn't display correct model.city
value.
htmlhelpers model values model state , not model when update , return model. in order update , return model, add line of code in post method:
modelstate.clear();
or set value of city in modelstate itself:
modelstate["city"].value = getcitybyzip(model.zip);
Comments
Post a Comment