如何知道的HtmlHelper数据上ViewBag? [英] How HtmlHelper know data is on the ViewBag?
问题描述
我在看关于的HtmlHelper
的下拉的 https://www.youtube.com/watch?v=79aYSOcmpV8
大约8分钟,他正在读DB更换一些硬code值。
要从控制器通过部门列表,将它们存储在ViewBag
块引用>公众的ActionResult指数()
{
//连接到数据库
SampleDBContext DB =新SampleDBContext();
//检索部门和建设的SelectList
ViewBag.Departments =新的SelectList(db.Departments,ID,姓名); 返回查看();
}最后一步。
现在,在指数来看,访问部门从ViewBag
列出
块引用>@ Html.DropDownList(部门,选择部门)
我不喜欢看到的视图强类型的模型东西。
结果
那么,如何在
助手
知道部门
是指在ViewBag值?解决方案当您添加值
的ViewBag
,它也被添加到了
ViewContext
中的ViewData code>属性时生成视图。您使用等同于传递一个空
的SelectList在的DropDownList()
超载@ Html.DropDownList(部门,NULL,选择部)
在这种情况下,在内部,助手搜索
的ViewData code>属性查找匹配的关键这是一个
的IEnumerable< SelectListItem>
(其中部门
是)。你可以看到的<一个在私有静态MvcHtmlString SelectInternal()
法的相关code href=\"https://aspnetwebstack.$c$cplex.com/SourceControl/latest#src/System.Web.Mvc/Html/SelectExtensions.cs\"相对=nofollow>来源$ C $ C//如果我们得到了一个空的选择列表,尝试使用的ViewData来获得的项目清单。
如果(选择列表== NULL)
{
选择列表= htmlHelper.GetSelectData(名);
....
}请注意,在教程中的例子是一个贫穷的方式,用神奇的字符串,并要求您在使用POST方法访问值
的Request.Form [部门]
。一个更好的方法是使用一个视图模型,并强烈绑定到你的浏览模式,例如:公共类MyViewModel
{
公众诠释SelectedDepartment {搞定;组; }
公共IEnumerable的&LT; SelectListItem&GT; DepartmentList {搞定;组; }
...
}和GET方法将是
公众的ActionResult的Create()
{
MyViewModel模式=新MyViewModel
{
DepartmentList =新的SelectList(db.Departments,ID,姓名);
};
返回查看(模型);
}和视图
@model MyViewModel
....
@ Html.DropDownListFor(M = GT; m.SelectedDepartment,Model.DepartmentList,选择部)和发布形式回
[HttpPost]
公众的ActionResult创建(MyViewModel模型)I was watching a tutorial about
HtmlHelper
for DropDown https://www.youtube.com/watch?v=79aYSOcmpV8Around min 8, he is reading the db to replace some hardcode values.
To pass list of Departments from the controller, store them in "ViewBag"
public ActionResult Index() { // Connect to the database SampleDBContext db = new SampleDBContext(); // Retrieve departments, and build SelectList ViewBag.Departments = new SelectList(db.Departments, "Id", "Name"); return View(); }
Last step.
Now in the "Index" view, access Departments list from "ViewBag"
@Html.DropDownList("Departments", "Select Department")
I dont see anything like strong type model on the view.
So how the
Helper
knowDepartments
refers to a value in the ViewBag?解决方案When you add a value to
ViewBag
, it is also added to theViewData
property ofViewContext
when the view is generated. TheDropDownList()
overload that your using in equivalent to passing anull
SelectList in@Html.DropDownList("Departments", null, "Select Department")
in which case, internally, the helper searches the
ViewData
property to find a matching key which is anIEnumerable<SelectListItem>
(which"Departments"
is). You can see the relevant code in theprivate static MvcHtmlString SelectInternal()
method of the source code// If we got a null selectList, try to use ViewData to get the list of items. if (selectList == null) { selectList = htmlHelper.GetSelectData(name); .... }
Note that the example in the tutorial is a poor approach, using 'magic' strings and requiring you to access the value in the POST method using
Request.Form["Departments"]
. A far better approach is to use a view model and strongly bind to your view model, for examplepublic class MyViewModel { public int SelectedDepartment { get; set; } public IEnumerable<SelectListItem> DepartmentList { get; set; } ... }
and the GET method would be
public ActionResult Create() { MyViewModel model = new MyViewModel { DepartmentList = new SelectList(db.Departments, "Id", "Name"); }; return View(model); }
and in the view
@model MyViewModel .... @Html.DropDownListFor(m => m.SelectedDepartment, Model.DepartmentList, "Select Department")
and post the form back to
[HttpPost] public ActionResult Create(MyViewModel model)
这篇关于如何知道的HtmlHelper数据上ViewBag?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!