asp.net mvc - How to bind value from database to Dropdown List in MVC 4 -
this model
public class insertorder { public int customerid { get; set; } public string customername { get; set; } } public list<insertorder> getcustomerid() { list<insertorder> customerlist = new list<insertorder>(); sqlconnection connection = new sqlconnection(configurationmanager.connectionstrings["myconnection"].connectionstring); sqldatareader datareader; { using (sqlcommand command = new sqlcommand("select customerid,firstname customers", connection)) { connection.open(); datareader = command.executereader(); while (datareader.read()) { insertorder id = new insertorder(); id.customerid = convert.toint32(datareader["customerid"]); id.customername = datareader["firstname"].tostring(); customerlist.add(id); } datareader.close(); return customerlist; } } }
how give in view page
public actionresult index() { var customerlist = new insertorder().getcustomerid(); viewbag.customerlist = new selectlist(customerlist, "customerid",customername") ; return view(yourmodel); }
at view
@html.dropdownlistfor(m => m.sampledropdownvalue, model.customerlist , "--select--")
Comments
Post a Comment