Rahil
Rahil

Reputation: 283

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 ?

Upvotes: 0

Views: 1395

Answers (2)

mmg666
mmg666

Reputation: 353

What about using ViewBag.businessTypeList for the first one and ViewBag.stateList for the second, which would also make the code much more readable?

Upvotes: 1

John Koerner
John Koerner

Reputation: 38077

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.

Upvotes: 0

Related Questions