c# - MVC.NET the ViewModel from Create POST is always null -
in mvc.net project used scaffolding templates. ther binded 1 dto model. decided wanted link viewmodel, because have 2 multiselects need use pass values. how viewmodel looks:
public class createquestionmodel { public question question { get; set; } public list<int> politicianids { get; set; } public list<int> topicids { get; set; } }
my create post method getting viewmodel view:
[httppost] [validateantiforgerytoken] [authorize(roles = "regular")] public actionresult create(createquestionmodel question) { if (modelstate.isvalid) { int id = websecurity.currentuserid; manager.createquestion(question.question, id, question.politicianids, question.topicids); return redirecttoaction("index"); } return view(question); }
and create.cshtml looks this:
@model politicionline.models.createquestionmodel @{ viewbag.title = "stel een vraag!"; } <head> <link rel="stylesheet" href="~/content/questions.css" type="text/css" /> @scripts.render("~/bundles/jquery") @scripts.render("~/bundles/jqueryval") <script src="@url.content("~/scripts/extra/chosen/chosen.jquery.min.js")" type="text/javascript"></script> <link rel="stylesheet" href="@url.content("~/scripts/extra/chosen/chosen.min.css")" type="text/css"> <script src="@url.content("~/scripts/extra/select2-3.4.6/select2.min.js")" type="text/javascript"></script> <link rel="stylesheet" href="@url.content("~/scripts/extra/select2-3.4.6/select2.css")" type="text/css"> </head> <h2>stel een vraag!</h2> @using (html.beginform()) { @html.antiforgerytoken() @html.validationsummary(true) <fieldset> <legend>vraag</legend> <div class="general-question"> <div class="editor-label"> @html.labelfor(model => model.question.generalquestion, "algemene vraag") </div> <div class="editor-field"> @html.textbox("question.generalquestion", new { @class = "general-question-edit" }) @html.validationmessagefor(model => model.question.generalquestion) </div> </div> <div id="geadresseerde-politici"> @html.labelfor(model => model.politicianids, "geadresseerde politicians:") @html.listbox("politicianids", (multiselectlist)viewbag.politicians, new { @id = "poldrop" }) </div> <div class="editor-label"> @html.labelfor(model => model.question.explanation, "extra uitleg") </div> <div class="editor-field"> @html.textarea("question.explanation", new { @class = "explanation-textarea-edit" }) @html.validationmessagefor(model => model.question.explanation) </div> <div> @html.labelfor(model => model.topicids, "kies je thema's (maximum 2):") @html.listbox("topicids", (multiselectlist)viewbag.topics, new { @id = "select2select", @style = "width: 500px"}) </div> <p> <input type="submit" value="indienen!" /> </p> </fieldset> } <div> @html.actionlink("back list", "index") </div> <script type="text/javascript"> function format(topic) { if (topic.css == 'optiongroup') { return "<b>" + topic.text + "</b>"; } else { return "<i> " + topic.text + "<i>"; } } $("#select2select").select2({ placeholder: "selecteer een thema...", maximumselectionsize: 2, formatresult: format, escapemarkup: function(m) { return m; } }); </script>
the <script>
section @ bottom doesn't matter pasted anyway, i'm using jquery plugin select2 listbox.
this way of binding textboxes , such viewmodel properties, found on stackoverflow. tried classic way using @html.editorfor
, @htmllistboxfor
viewmodel's properties null.
what doing wrong/ overlooking?
edit: put constructor in viewmodel, viewmodel (createquestionmodel) not null anymore, values still default values (not ones form). viewmodel looks like:
public class createquestionmodel { public question question { get; set; } public list<int> politicianids { get; set; } public list<int> topicids { get; set; } public createquestionmodel() { question = new question(); politicianids = new list<int>(); topicids = new list<int>(); } }
solution commenter yoeri provided solution, can see below in answer on question!
okay guys, found out doing wrong. made extremely stupid mistake, can't believe i've been struggling 2 days now.
the signature of create post method was:
public actionresult create(createquestionmodel question) { ... }
it should be:
public actionresult create(createquestionmodel createquestionmodel) { ... }
i had change parameter form createquestionmodel question
createquestionmodel createquestionmodel
, simple that, works.
update yoeri understand why giving problems in first place:
initially named parameter createquestionmodel question
, didn't work because have model class named question
. parameter name has either name of model using in create view, or other name long it's not name of model class!
thanks yoeri!
Comments
Post a Comment