如何在mvc中使用不同的模型填充下拉列表
问题描述:
我有一个View和一个与之对应的模型。我的问题是我想在视图中使用同一视图中的不同模型填充下拉列表。我寻求你的帮助
I have a View and a Model corresponding to it.My problem is I want to populate drop downlist in the view using a different model in the same view.I seek your help
答
使用Viewbag
as
use Viewbag
as
public ActionResult PPEntry()
{
ViewBag.ddlData = new FillMaster().GetddList();
//your model code
return View(model);
}
在您的视图中
in your View
Select Block <select id="ddlMaster">
@foreach (var item in ViewBag.ddlData)
{
<option value="@item.ID">@item.STATE</option>
}
</select>
由于Model只是一个类..您可以在View中使用当前Model中的那个类属性并使用它其他模型类属性在您的下拉列表中指定值..
希望这有助于..!
As the Model is only a class.. You can use that class property inside you current Model which you are referring in the View and use that other model class property to assign the values in your drop down..
Hope this helps..!