c# - To pass value in DropDrownList to the Controller using ajax -
i have 2 textboxes , 1 dropdownlist. have passed value of textboxes controller action method using id through ajax.beginform() method. how pass value of dropdownlist has not defined id. here code:
deliveryindex.cshtml:
@using (ajax.beginform("customerfilter", "delivery", new system.web.mvc.ajax.ajaxoptions { insertionmode = system.web.mvc.ajax.insertionmode.replace, httpmethod = "post", updatetargetid = "deliverylist" })) { <input id="from" name="from" type="text" /> <input id="to" name="to" type="text" /> @html.dropdownlist("cus_name",null,"--select customer--",new { @style = "width:169px" }) <input type="submit" value="view" id="btnsubmit" /> }
controller:
public class deliverycontroller : mastercontroller { public actionresult deliveryindex() { viewbag.cus_name = new selectlist(db.customer_master.tolist().orderby(c => c.cus_name), "cus_name", "cus_name"); return view("deliveryindex"); } [httppost] public actionresult customerfilter(string from, string to, string ) { .... } }
based upon example parameter should available cus_name
.
changing action to:
[httppost] public actionresult customerfilter(string from, string to, string cus_name) { .... }
will pass through, advise using typed view model.
as example:
view model
public class fooviewmodel { public string { get;set; } public string { get;set; } // represents selected value public string cusname { get;set; } public list<customer> availablecustomers { get;set;} } public class customer { public int id { get;set;} public string name { get;set;} }
action
[httppost] public actionresult customerfilter(fooviewmodel model) { .... }
view
@model fooviewmodel @using (ajax.beginform("customerfilter", "delivery", new system.web.mvc.ajax.ajaxoptions { insertionmode = system.web.mvc.ajax.insertionmode.replace, httpmethod = "post", updatetargetid = "deliverylist" })) { @html.textboxfor(m => m.from) @html.textboxfor(m => m.to) @html.dropdownlistfor(m => m.cusname, new selectlist(model.availablecustomers, "id", "name"),"--select customer--",new { @style = "width:169px" }) <input type="submit" value="view" id="btnsubmit" /> }
update per stephen muecke's comments, have updated model include list of options too.
Comments
Post a Comment