c# - Unable to bind MVC model property to input element -
i'm having trouble getting model bind input box created custom helper method. wrapped jquery ajax-driven autocomplete box helper method called "html.autocompleteboxajax." helper creates element javascript autocomplete functionality.
the property in model string called "formatname". have verified that, in generated html view, both name , id of input element "formatname", , there no other elements identities. i've checked model has default constructor, , "formatname" property publicly accessible. lastly, i've verified when channel model passed view, channel.formatname has correct value. yet despite this, can't value bind element, , input box remains blank. there no binding when going other way, view controller, , channel.formatname remains blank.
what missing? somehow because i'm using custom helper method?
model:
namespace webapp.models { public class channelmodel { xyz.dataaccess.ods.db db = config.db(); public string id { get; set; } // parent project [display(name = "project")] public string projectid { get; set; } // format name [required(errormessage = "format required.")] [regularexpression(constants.username_regex, errormessage = constants.username_regexerrormsg)] [display(name = "format")] public string formatname { get; set; } // channel name [required(errormessage="channel name required.")] [stringlength(100, errormessage = constants.minlengtherrormsg, minimumlength = constants.username_minlength)] [regularexpression(constants.username_regex, errormessage = constants.username_regexerrormsg)] [display(name = "channel name")] public string name { get; set; } // sequence [display(name = "sequence")] public string sequenceid { get; set; } public channelmodel() { id = guid.newguid().tostring(); } public channelmodel(xyz.dataaccess.ods.channel channel_db) { id = channel_db.id; projectid = channel_db.project_id; name = channel_db.name; formatname = channel_db.format_name; sequenceid = channel_db.sequence_id; } public xyz.dataaccess.ods.channel builddbobject() { xyz.dataaccess.ods.channel channel = new xyz.dataaccess.ods.channel(); channel.id = id; channel.project_id = projectid; channel.name = name; channel.format_name = formatname; channel.sequence_id = sequenceid; return channel; } } } view
@model webapp.models.channelmodel @using helpermethods.infrastructure @{ viewbag.title = "edit"; var sequences = viewdata["sequences"] list<selectlistitem>; } <h2>edit</h2> <script src="@url.content("~/scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@url.content("~/scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @section header { } @using (html.beginform()) { @html.validationsummary(true) <fieldset> <legend>channel</legend> @html.hiddenfor(model => model.id) @html.hiddenfor(model => model.projectid) @html.hiddenfor(model => model.name) <div class="editor-field"> @html.labelfor(model => model.name): @html.displayfor(model => model.name) </div> <div class="editor-label"> @html.label("format") </div> <div class="editor-field"> @html.autocompleteboxajax("formatname", url.action("getformatsbeginningwith")) @html.validationmessagefor(model => model.formatname) </div> <!-- sequence --> <div class="editor-label"> @html.label("sequence") </div> <div class="editor-field"> @html.slavedropdownlist("sequenceid", "groupid", url.action("getsequencesingroup"), webapp.util.makeselectlist(sequences, model.sequenceid)) </div> <p> <input type="submit" value="save" /> </p> </fieldset> } <div> @html.actionlink("back list", "index") </div> controller
namespace webapp.controllers { public class channelcontroller : infrastructure.nocachecontroller { xyz.dataaccess.ods.db db = config.db(); -- stuff -- [httpget] public actionresult getformatsbeginningwith(string term) { var formats = db.getformatsbeginningwith(term); list<customhelpers.autocompleteitem> items = new list<customhelpers.autocompleteitem>(); foreach (var format in formats) items.add(new customhelpers.autocompleteitem { value = format.name, label = format.name }); var j = json(items, jsonrequestbehavior.allowget); return j; } public actionresult edit(string id) { channelmodel channelmodel = new channelmodel(db.getchannel(id)); string groupid = db.getproject(channelmodel.projectid).group_id; var sequences = db.getsequencesingroup(groupid); viewdata["sequences"] = makeselectlistitems(sequences); return view(channelmodel); } // // post: /channel/edit/5 [httppost] public actionresult edit(channelmodel model) { if (modelstate.isvalid) { db.updatechannel(model.builddbobject()); return redirecttoaction("index"); } string groupid = db.getproject(model.projectid).group_id; var sequences = db.getsequencesingroup(groupid); viewdata["sequences"] = makeselectlistitems(sequences); return view(model); } -- more stuff -- } } helper method autocompletebox
public static mvchtmlstring autocompleteboxajax(this htmlhelper html, string id, string actionurl) { tagbuilder input = new tagbuilder("input"); input.attributes.add("id", id); input.attributes.add("name", id); input.attributes.add("type", "text"); input.addcssclass("autocomplete_ajax"); input.attributes.add("value", ""); input.attributes.add("action", actionurl); var variables = new dictionary<string, string>() { {"autocomplete_id", id} }; var script = populatescripttemplate("template_autocomplete_ajax.js", variables); stringbuilder s = new stringbuilder(); s.appendline(input.tostring(tagrendermode.selfclosing)); s.appendline(script); return new mvchtmlstring(s.tostring()); } javascript autocomplete
$('#autocomplete_id').autocomplete({ source: $('#autocomplete_id').attr('action') }); relevant portion of view's html
<div class="editor-field"> <input action="/channel/getformatsbeginningwith" class="autocomplete_ajax" id="formatname" name="formatname" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="formatname" data-valmsg-replace="true"></span> </div>
answer turned out simple input @jakub. helper has populate value; there's no automatic binding takes place html helpers. once took care of this, auto binding of posts controller worked expected.
Comments
Post a Comment