简体   繁体   中英

using two drop down list on same page dot net mvc razor

I have two drop down lists on same web page:

View:

@Html.DropDownList("businessType", (SelectList) ViewBag.list, " -- Select Business Type -- ") and 
@Html.DropDownList("state", (SelectList) ViewBag.list, " -- Select State -- ")

and controller:

var query2 = db.Database.SqlQuery<statedropDownModel>("stateDropDownSP");
ViewBag.list = new SelectList(query2.AsEnumerable(), "Code", "StateName", "--select2--");

var query = db.Database.SqlQuery<businessDropDownModel>("businesDropDownSP");
ViewBag.list = new SelectList(query.AsEnumerable(), "ID", "BusinessTypeName", "--select--");

Now Problem is that, these two drop down lists are population dynamically with different data, but, it got conflict and same data is being populated in both the list. Any solution to it ?

如何将ViewBag.businessTypeList用于第一个,并将ViewBag.stateList用于第二个,这也将使代码更具可读性?

You are re-using the ViewBag.list variable, so you are only getting the values from the second query.

I would suggest returning this data in the model for your view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM