asp.net mvc 4 dropdownlist does not return a value -
i have 2 models - question , category -
public class question { [scaffoldcolumn(false)] public int questionid { get; set; } [required] public string questiontext { get; set; } [required] public string answera { get; set; } [required] public string answerb { get; set; } [required] public string answerc { get; set; } [required] public string answerd { get; set; } [required] public int correct { get; set; } [foreignkey("category")] [display(name = "category")] [required] public int categoryid; //navigation property public virtual category category { get; set; } } public class category { [scaffoldcolumn(false)] public int categoryid { get; set; } [required] public string name { get; set; } public virtual icollection<question> question { get; set; } }
in questioncontroller, have added code able access available categories dropdownlist in view -
private void populatecategorydropdownlist(object selectedcategory = null) { var categoryquery = c in db.categories orderby c.name select c; viewbag.categoryid = new selectlist(categoryquery, "categoryid", "name", selectedcategory); }
and have following methods create -
// get: /question/create public actionresult create() { populatecategorydropdownlist(); return view(); } // // post: /question/create [httppost] [validateantiforgerytoken] public actionresult create(question question) { try { var errors = modelstate.values.selectmany(v => v.errors); if (modelstate.isvalid) { db.questions.add(question); db.savechanges(); return redirecttoaction("index"); } } catch (dataexception dex) { modelstate.addmodelerror("",dex.message); } populatecategorydropdownlist(question.category.categoryid); return view(question); }
my view creating new question follows - @model quiz.models.question
@{ viewbag.title = "create"; } @using (html.beginform()) { @html.antiforgerytoken() @html.validationsummary(true) <fieldset> <legend>question</legend> <div class="editor-label"> @html.labelfor(model => model.questiontext) </div> <div class="editor-field"> @html.editorfor(model => model.questiontext) @html.validationmessagefor(model => model.questiontext) </div> <div class="editor-label"> @html.labelfor(model => model.answera) </div> <div class="editor-field"> @html.editorfor(model => model.answera) @html.validationmessagefor(model => model.answera) </div> <div class="editor-label"> @html.labelfor(model => model.answerb) </div> <div class="editor-field"> @html.editorfor(model => model.answerb) @html.validationmessagefor(model => model.answerb) </div> <div class="editor-label"> @html.labelfor(model => model.answerc) </div> <div class="editor-field"> @html.editorfor(model => model.answerc) @html.validationmessagefor(model => model.answerc) </div> <div class="editor-label"> @html.labelfor(model => model.answerd) </div> <div class="editor-field"> @html.editorfor(model => model.answerd) @html.validationmessagefor(model => model.answerd) </div> <div class="editor-label"> @html.labelfor(model => model.correct) </div> <div class="editor-field"> @html.editorfor(model => model.correct) @html.validationmessagefor(model => model.correct) </div> <div class="editor-label"> @html.labelfor(model => model.categoryid) </div> <div class="editor-field"> @html.dropdownlistfor(model => model.categoryid,(selectlist)viewbag.categoryid) @html.validationmessagefor(model => model.categoryid) </div> <p> <input type="submit" value="create" /> </p> </fieldset> } <div> @html.actionlink("back list", "index") </div> @section scripts { @scripts.render("~/bundles/jqueryval") }
so, issue although question can created, categoryid dropdownlist null.
i have tried bunch of things, ranging attempting access dropdownlist directly creating different viewmodel. however, none of them work required. also, code follows tutorials available online. i'm not able figure out different.
please me find mistake in code.
we might have narrow things down, have feeling going wrong models being read correctly viewbag. try replacing create
actions moment following, custom viewbag filling function has been removed:
public actionresult create() { viewbag.categoryid = new selectlist(db.categories, "categoryid", "name"); return view(); } [httppost] [validateantiforgerytoken] public actionresult create(question question) { try { var errors = modelstate.values.selectmany(v => v.errors); if (modelstate.isvalid) { db.questions.add(question); db.savechanges(); return redirecttoaction("index"); } } catch (dataexception dex) { modelstate.addmodelerror("",dex.message); } viewbag.categoryid = new selectlist(db.categories, "categoryid", "name", question.category.categoryid); return view(question); }
does run correctly?
if doesn't work must model-binding issue. last thing can think of trying change viewbag calls effect field category
instead of categoryid
. update view when making dropdownlist.
Comments
Post a Comment