c# - How can I send DropDownList's SelectedValue to the Controller from View with BeginForm? -
how can send dropdownlist's selectedvalue controller view beginform?
here's code:
@using (html.beginform(new { newvalue=ddl.selectedvalue})) { @html.dropdownlist("categories", (list<selectlistitem>)viewdata["categories"], new { onchange = "this.form.submit()", id = "ddl" })
do not utilize viewdata or viewbag in place of model. it's sloppy, prone error , unorganized way of giving view data.
{ newvalue=ddl.selectedvalue} going nil when placed on form itself. need understand you're writing evaulated on server before beingness sent downwards client. if newvalue resolves 1 go on remain 1 forever unless have javascript changes on clientside (which you're not doing , shouldn't doing).
first need model:
public class categorymodel() { public ienumberable<selectlistitem> categorieslist {get;set;} public int selectedcategoryid {get;set;} } controller
public class categorycontroller() { public actionresult index() { var model = new categorymodel(); model.categorieslist = new list<selectlistitem>{...}; homecoming view(model); } public actionresult savecategory(categorymodel model) { model.selectedcategoryid ... } } view
@model categorymodel @using(html.beginform("savecategory","category")) { @html.dropdownlistfor(x=> x.selectedcategoryid, model.categorieslist) <button type="submit">submit</button> } what's happening here selectlist beingness populated ienumerable , it's form name selectedcategoryid, that's posed server.
i'm not sure knowledge of http , html ends, should not using framework until understand how http , html work , these helpers such begin form , html.dropdownlist doing you. understand how screw works before seek utilize screw driver.
c# asp.net-mvc entity-framework
No comments:
Post a Comment