这是什么线code ViewBag.RoleId =新的SelectList的(RoleManager.Roles," ID和QUOT;,"姓名和QUOT;) [英] What is this line of code ViewBag.RoleId = new SelectList(RoleManager.Roles, "Id", "Name")
问题描述
我在code例如找到下载code的下面一行在控制器的操作方法。
ViewBag.RoleId =新的SelectList(RoleManager.Roles,ID,姓名)或可能是它的异步版本
ViewBag.RoleId =新的SelectList(等待RoleManager.Roles.ToListAsync(),ID,姓名);
我无法捉摸什么发生在这里。另外在相应的观点,我无处可以看到使用ViewBag.RoleId。相反,我觉得在视图
@ Html.DropDownList(角色ID,无作用)
有似乎是在两者之间一些琐碎连接。有人可以抛出一些轻磨在我丢失。
以下是code的操作方法就行了。
公共异步任务<&的ActionResult GT;创建()
{
//获取角色列表
ViewBag.RoleId =新的SelectList(等待RoleManager.Roles.ToListAsync(),ID,姓名);
返回查看();
}
和相应的视图如下:
@model AspnetIdentitySample.Models.RegisterViewModel
@ {
ViewBag.Title =创建;
}< hgroup类=标题>
< H1方式> @ ViewBag.Title< / H1>
< / hgroup>@using(Html.BeginForm())
{
@ Html.AntiForgeryToken()
@ Html.ValidationSummary() <字段集类=形横>
<传奇>创建一个新的用户LT; /传说>
< DIV CLASS =控制组>
@ Html.LabelFor(M = GT; m.UserName,新{@class =控制标签})
< DIV CLASS =控制>
@ Html.TextBoxFor(M = GT; m.UserName)
< / DIV>
< / DIV>
< DIV CLASS =控制组>
@ Html.LabelFor(M = GT; m.Password,新{@class =控制标签})
< DIV CLASS =控制>
@ Html.PasswordFor(M = GT; m.Password)
< / DIV>
< / DIV>
< DIV CLASS =控制组>
@ Html.LabelFor(M = GT; m.ConfirmPassword,新{@class =控制标签})
< DIV CLASS =控制>
@ Html.PasswordFor(M = GT; m.ConfirmPassword)
< / DIV>
< / DIV> < DIV CLASS =控制组>
@ Html.LabelFor(M = GT; m.HomeTown,新{@class =控制标签})
< DIV CLASS =控制>
@ Html.TextBoxFor(M = GT; m.HomeTown)
< / DIV>
< / DIV>
< H4>选择用户和LT的作用; / H4>
<小时/>
@ Html.DropDownList(角色ID,无作用)
< DIV CLASS =表单行为无颜色>
<输入类型=提交值=创建类=BTN/>
< / DIV>
< /字段集>
}@section脚本{
@ Scripts.Render(〜/包/ jqueryval)
}
这行
ViewBag.RoleId =新的SelectList(RoleManager.Roles,ID,姓名)
是创建一个由 @ Html.DropDownList()
或 @使用
助手来呈现在的SelectList
Html.DropDownListFor()&LT的选项;选择>
标记。该选项将有一个值
按角色的
并通过编号
属性定义属性名称定义的显示文本
属性。
该视图中使用
@ Html.DropDownList(角色ID,无作用)
其中结合的ViewData属性角色ID
(即 ViewBag
属性),第二个参数
渲染选项标签(该第一个选项将没有任何价值,但显示文本无角色
这是不是一个很好的使用情况,并建议您使用强类型的辅助绑定到你的模型属性。
ViewBag.RoleList =新的SelectList(RoleManager.Roles,ID,姓名)@ Html.DropDownListFor(M = GT; m.RoleID,(的SelectList)ViewBag.RoleList,无作用)
其中,角色ID
是在模型的属性将被绑定到所选择的选项值
I find in a code example downloaded the following line of code in an Action method of a controller. ViewBag.RoleId = new SelectList(RoleManager.Roles, "Id", "Name") or probably its async version ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Id", "Name");
I am not able to fathom whats happening here. Also in the corresponding view, I nowhere see ViewBag.RoleId being used. Instead I find in the view @Html.DropDownList("RoleId","No Roles")
There appears to be some trivial connection between the two. Can someone throw some light at whet I am missing.
The following is the line of code in the action method.
public async Task<ActionResult> Create()
{
//Get the list of Roles
ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Id", "Name");
return View();
}
And the corresponding view is as follows.
@model AspnetIdentitySample.Models.RegisterViewModel
@{
ViewBag.Title = "Create";
}
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
</hgroup>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset class="form-horizontal">
<legend>Create a new user.</legend>
<div class="control-group">
@Html.LabelFor(m => m.UserName, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(m => m.UserName)
</div>
</div>
<div class="control-group">
@Html.LabelFor(m => m.Password, new { @class = "control-label" })
<div class="controls">
@Html.PasswordFor(m => m.Password)
</div>
</div>
<div class="control-group">
@Html.LabelFor(m => m.ConfirmPassword, new { @class = "control-label" })
<div class="controls">
@Html.PasswordFor(m => m.ConfirmPassword)
</div>
</div>
<div class="control-group">
@Html.LabelFor(m => m.HomeTown, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(m => m.HomeTown)
</div>
</div>
<h4>Select Role for User</h4>
<hr />
@Html.DropDownList("RoleId","No Roles")
<div class="form-actions no-color">
<input type="submit" value="Create" class="btn" />
</div>
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
This line
ViewBag.RoleId = new SelectList(RoleManager.Roles, "Id", "Name")
is creating a SelectList
used by the @Html.DropDownList()
or @Html.DropDownListFor()
helpers to render the options in a <select>
tag. The options will have a value
attribute defined by the Id
property of Role
and a display text defined by the Name
property.
The view is using
@Html.DropDownList("RoleId","No Roles")
which binds to ViewData property RoleId
(the ViewBag
property) and the second argument renders an option label (the first option will have no value, but display the text "No Roles"
This is not a good usage, and it is recommended you use strongly typed helpers to bind to your model properties.
ViewBag.RoleList = new SelectList(RoleManager.Roles, "Id", "Name")
@Html.DropDownListFor(m => m.RoleID, (SelectList)ViewBag.RoleList,"No Roles")
where RoleID
is a property in the model that will be bound to the selected options value
这篇关于这是什么线code ViewBag.RoleId =新的SelectList的(RoleManager.Roles,&QUOT; ID和QUOT;,&QUOT;姓名和QUOT;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!