asp.net mvc 3 - MVC 3 Model binding and No parameterless constructor defined for this object -
i created view working wonderfully until added jquery support cascading drop downs. believe in doing that, broke binding between view , model. i'm getting error "no parameterless constructor defined object." when form submitted. obvious solution add parameterless constructor, i'm assuming postmodel null? code snippets below.
thanks in advance help.
view:
<script type="text/javascript"> $(document).ready(function () { $("#ddlcategories").change(function () { var icategoryid = $(this).val(); $.getjson( "@url.content("~/remote/subcategoriesbycateogry")", { id: icategoryid }, function (data) { var select = resetandreturnsubcategoryddl(); $.each(data, function (index, itemdata) { select.append($('<option/>', { value: itemdata.value, text: itemdata.text })); }); }); }); function resetandreturnsubcategoryddl() { var select = $('#ddlsubcategory'); select.empty(); select.append($('<option/>', { value: '', text: "--select subcategory--" })); return select; } });
...
<div class="editor-field"> @html.dropdownlist("icategoryid", model.categories,"--select category--", new dictionary<string,object>{ {"class","dropdowns"},{"id","ddlcategories"}}) @html.validationmessage("icategoryid") </div> <div class="editor-label"> @html.labelfor(model => model.subcategories, "subcategory") </div> <div class="editor-field"> @html.dropdownlistfor(model => model.subcategories, new selectlist(enumerable.empty<selectlistitem>(), "isubcategoryid", "subcategory",model.subcategories), "--select subcategory--", new { id = "ddlsubcategory" }) @html.validationmessage("isubcategoryid") </div>
controller:
[httppost] public actionresult create(vendorcreatemodel postmodel) { vendorcreateeditpostvalidator createvalidator = new vendorcreateeditpostvalidator( postmodel.icategoryid, postmodel.isubcategoryid, postmodel.appliedprograms, m_unitofwork.programrepository, new modelstatevalidationwrapper(modelstate)); if (modelstate.isvalid) { int categoryid = int.parse(postmodel.icategoryid); int subcategoryid = int.parse(postmodel.isubcategoryid); var programids = postmodel.appliedprograms.select(ap => int.parse(ap)); var programs = m_unitofwork.programrepository.getprograms(programids); vendor vendor = postmodel.vendor; vendor.category = m_unitofwork.categoryrepository.getcategory(categoryid); vendor.subcategory = m_unitofwork.subcategoryrepository.getsubcategory(subcategoryid); foreach (program p in programs) vendor.programs.add(p); m_unitofwork.vendorrepository.add(vendor); m_unitofwork.savechanges(); return redirecttoaction("index"); } vendorcreatemodel model = new vendorcreatemodel( postmodel.vendor, postmodel.icategoryid, postmodel.isubcategoryid, postmodel.appliedprograms, user.identity.name, m_unitofwork.categoryrepository, m_unitofwork.subcategoryrepository, m_unitofwork.permissionrepository); return view(model); }
remotecontroller:
[acceptverbs(httpverbs.get)] public jsonresult subcategoriesbycateogry(int id) { system.diagnostics.debug.writeline(id); var subcategories = db.subcategories .where(v => v.icategoryid == id) .orderby(v => v.sdesc) .tolist(); var modeldata = subcategories.select(v => new selectlistitem() { text = v.sdesc, value = v.isubcategoryid.tostring() }); return json(modeldata, jsonrequestbehavior.allowget); }
vendorcreatemodel:
public class vendorcreatemodel { public vendorcreatemodel() { } public vendorcreatemodel( vendor vendor, string categoryid, string subcategoryid, ienumerable<string> appliedprograms, string username, icategoryrepository categoryrepository, isubcategoryrepository subcategoryrepository, ipermissionrepository permissionrepository) { userhasprogramsvalidator programvalidator = new userhasprogramsvalidator(username, permissionrepository); var availableprograms = programvalidator.availableprograms; hashset<category> applicablecategories = new hashset<category>(); foreach (var p in availableprograms) foreach (var c in categoryrepository.getcategoriesbyprogram(p.iprogramid)) applicablecategories.add(c); this.vendor = vendor; this.appliedprograms = appliedprograms; this.categories = new selectlist(applicablecategories.orderby(x => x.sdesc).tolist(), "icategoryid", "sdesc"); this.subcategories = new selectlist(subcategoryrepository.getallsubcategories().orderby(x => x.sdesc).tolist(), "isubcategoryid", "sdesc"); if (!string.isnullorempty(categoryid)) { int temp; if (!int.tryparse(categoryid, out temp)) throw new applicationexception("invalid category identifier."); } this.icategoryid = categoryid; this.isubcategoryid = subcategoryid; this.programitems = availableprograms .select(p => new selectlistitem() { text = p.sdesc, value = p.iprogramid.tostring(), selected = (appliedprograms != null ? appliedprograms.contains(p.iprogramid.tostring()) : false) }); } public vendor vendor { get; set; } public selectlist categories { get; set; } public selectlist subcategories { get; set; } public string icategoryid { get; set; } public string isubcategoryid { get; set; } public ienumerable<selectlistitem> programitems { get; set; } [atleastoneelementexists(errormessage = "please select @ least 1 program.")] public ienumerable<string> appliedprograms { get; set; } }
i correct issue , wanted share in case else banging head against desk ihave been. changed dropdownlistfor reflect:
@html.dropdownlistfor(model => model.isubcategoryid, new selectlist(enumerable.empty<selectlistitem>(), "isubcategoryid", "subcategory",model.subcategories), "--select subcategory--", new dictionary<string,object>{ {"class","dropdowns"},{"id","ddlsubcategory"},{"name","isubcategoryid"}})
Comments
Post a Comment