我有一个包含几个下拉列表的MVC 4局部视图但是当我在索引和其他页面中使用这个局部视图时,它要求我提供下拉源数据的视图包设置。我的问题是,我可以有一个局部视图,它将从其自己的控制器加载所有相关数据,我不必担心我使用它的所有十个页面中的数据源填充。所以请告知。
答案 0 :(得分:0)
您可以创建ApplicationController
或BaseController
并在其中填写下拉列表,并在partial
中使用它。例如,见:
ApplicationController
public class ApplicationController : Controller
{
public ApplicationController()
{
///RoomNum list
List<SelectListItem> RoomNumitems = new List<SelectListItem>();
RoomNumitems.Add(new SelectListItem
{
Text = "1",
Value = "1"
});
RoomNumitems.Add(new SelectListItem
{
Text = "2",
Value = "2"
});
RoomNumitems.Add(new SelectListItem
{
Text = "3",
Value = "3"
});
RoomNumitems.Add(new SelectListItem
{
Text = "4",
Value = "4"
});
ViewData["RoomNumListItems"] = RoomNumitems;
///
///From_Price list
List<SelectListItem> From_Price_items = new List<SelectListItem>();
From_Price_items.Add(new SelectListItem
{
Text = "30000",
Value = "30000"
});
From_Price_items.Add(new SelectListItem
{
Text = "50000",
Value = "50000"
});
From_Price_items.Add(new SelectListItem
{
Text = "70000",
Value = "70000"
});
From_Price_items.Add(new SelectListItem
{
Text = "100000",
Value = "100000"
});
ViewData["From_PriceListItems"] = From_Price_items;
///
///To_Price list
List<SelectListItem> To_Price_items = new List<SelectListItem>();
To_Price_items.Add(new SelectListItem
{
Text = "100000",
Value = "100000"
});
To_Price_items.Add(new SelectListItem
{
Text = "150000",
Value = "150000"
});
To_Price_items.Add(new SelectListItem
{
Text = "250000",
Value = "250000"
});
To_Price_items.Add(new SelectListItem
{
Text = "500000",
Value = "500000"
});
To_Price_items.Add(new SelectListItem
{
Text = "1000000",
Value = "1000000"
});
ViewData["To_PriceListItems"] = To_Price_items;
}
}
部分视图
<div class="row">
<div class="large-4 columns admin-lbl-fildset push-8 columns">
@Html.Label("Room number:")
</div>
<div class="large-8 columns admin-txt pull-4 columns">
@Html.DropDownList("RoomNum_SelectedItem", (IEnumerable<SelectListItem>)ViewData["RoomNumListItems"], new { @class = "admin-dropdown" })
</div>
</div>
<div class="row">
<div class="large-3 columns admin-lbl-fildset push-3 columns">
@Html.Label("To:")
</div>
<div class="large-3 columns admin-txt pull-3 columns">
@Html.DropDownList("To_Price_SelectedItem", (IEnumerable<SelectListItem>)ViewData["To_PriceListItems"], new { @class = "admin-dropdown" })
</div>
<div class="large-3 columns admin-lbl-fildset push-3 columns">
@Html.Label("From:")
</div>
<div class="large-3 columns admin-txt pull-3 columns">
@Html.DropDownList("From_Price_SelectedItem", (IEnumerable<SelectListItem>)ViewData["From_PriceListItems"], new { @class = "admin-dropdown" })
</div>
</div>
Cotrollers(例如主页)
public class HomeController : ApplicationController
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
}