c# - MVC DropDown Lists Complex Parent Model -
i'm guessing i've missed simple principle here point out rather quickly. have person object contains basic properties , couple of lists<> phones , addresses. phone , address each have type (home/work/etc.). addresses has state , county.
when i'm executing manage action, i'm setting viewbag countyid, stateprovinceid, etc. when have second address or phone, these dropdowns don't seem have correct selected value.
i'm sure pathetically basic i'm doing wrong here...
resolved...
i knew there basic missing , answer create custom view models address , phone objects. these new view models contain list each of of these select lists...
model sample
public customermodel() { isactive = true; if (phones == null) { phones = new list<phone>(); } if (addresses == null) { addresses = new list<address>(); } } public guid id { get; set; } public guid organizationid { get; set; } [required] public string firstname { get; set; } public string middlename { get; set; } [required] public string lastname { get; set; } [regularexpression(".+\\@.+\\..+", errormessage = "please enter valid email address.")] public string emailaddress { get; set; } [datatype(datatype.date)] public datetime? dateofbirth { get; set; } public list<phone> phones { get; set; } public list<address> addresses { get; set; } }
parent view sample
<tbody id="tbladdressrows"> @foreach (var address in model.addresses) { html.renderpartial("_addressrow", address); } </tbody>
partial view sample
<td> @html.hiddenfor(a => a.id) @html.dropdownlist("addresstypeid", viewdata["addresstypeid"] selectlist) </td> <td class="addresslines"> @html.editorfor(a => a.addressline1) @html.editorfor(a => a.addressline2) </td> <td> @html.editorfor(a => a.city) </td> <td> @html.dropdownlistfor(m => m.stateprovinceid, viewdata["stateprovinceid"] selectlist, new {@class = "state"}) </td> <td> @html.editorfor(a => a.postalcode, new {@class = "zipcode"}) </td> <td> @html.dropdownlist("countyid", viewdata["countyid"] selectlist, new {@class = "county"}) </td>
controller sample
// // get: /customer/manage/5 public actionresult manage(guid id) { customermodel customer = dataservice.getcustomer(id, httpcontext.user.identity.name); if (customer == null) { return httpnotfound(); } viewbag.addresstypeid = new selectlist(dataservice.getorganizationaddresstypes(httpcontext.user.identity.name), "id", "name"); viewbag.phonetypeid = new selectlist(dataservice.getorganizationphonetypes(httpcontext.user.identity.name), "id", "name"); viewbag.stateprovinceid = new selectlist(dataservice.getstates(), "id", "name"); viewbag.countyid = new selectlist(dataservice.getcounties(0), "id", "name"); return view(customer); }
you have number of things wrong here, of them haven't noticed yet. will.
the first should never name dropdownlist's collection same selected value. mvc uses viewbag
, viewstate
in non-obvious ways , run lots of trouble if this. change list of items stateprovincelist
, countylist
, phonetypelist
, addresstypelist
.
the second thing unable post values controller correctly, because using partial view render sub-item of collection, , passing item model view. mvc renders objects based on model root, , creates names assuming case. if posting items action method accepts parent class, model binder not able bind partial rendered values correctly (because mvc rendered them if submodel, not collection of submodel).
you should instead using editortemplate, several things. first, editortemplate automatically iterate on collections, , second render names of form controls proper names take account parent class.
read more editortemplates here:
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html
aside:
why using dropdownlistfor stateprovince dropdownlist county , address? use typed versions if can, helps prevent form item misnaming.
another thing, why if block in constructor? definition, constructor gets called when constructing object, properties null (well, derive class , set properties in subclass constructor, if it's code know that)
Comments
Post a Comment