ASP.. Net MVC下拉列表编辑器模板
本文关键字:编辑器 下拉列表 Net MVC ASP | 更新日期: 2023-09-27 18:16:28
我创建了一个DropDownList EditorTemlate,当我将ViewBag传递给EditorTemlate(我已经将其绑定到视图模型)时,它可以正常工作。下面是我的代码片段。DropDownList:
@model dynamic
@{
Layout = "~/Views/Shared/EditorTemplates/_Layout.cshtml";
}
@Html.DropDownList("", (SelectList) ViewBag.DropDownList,"Select an option")
注册视图模型:
public class RegisterModel
{
[Required]
[DataType(DataType.Password)]
[Display(Name = "Confirm Password", Prompt = "Confirm Password")]
[Compare("Password", ErrorMessage = "The Password and Confirmation Password do not match")]
public string ConfirmPassword { get; set; }
[Required]
[Display(Name = "Question", Prompt = "Question")]
public string Question { get; set; }
[Required]
[Display(Name = "Answer",Prompt="Answer")]
public string Answer { get; set; }
[Required]
[UIHint("DropDownList")]
[Display(Name = "User Role", Prompt = "User Role")]
public IEnumerable<UserRoles> RoleId { get; set; }
}
My Controller:
[HttpGet]
public ActionResult Users() {
var DropDownListModel = (from mm in db.roles
orderby mm.RoleName
select mm).ToList();
ViewBag.DropDownList = new SelectList(DropDownListModel, "RoleId", "RoleName");
ViewBag.Content = from users in db.UserDetails
select users;
return View();
}
我的观点
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.EditorForModel()
<input type="submit" value="Create User" />
Html.EndForm();
}
结束更新
我的挑战是,这是绑定到一个单一的模型。我可以让我的下拉列表编辑器模板动态地接受一个模型(一个通用的下拉列表编辑器模板,不管项目),这样我将使用相同的下拉列表所有用户角色列表,产品列表,模块列表,…? 我已经被困了两天了。在设计时,所有的搜索似乎都与模型绑定在一起。
更多关于的信息假设您希望在相同视图中显示其他模型,或者其他视图作为下拉列表,那么您的想法是创建一个EditorTemplate,它可以接受模型作为参数,并根据传递给它的模型显示下拉列表。我不想为每个呈现不同模型的视图创建EditorTemplate。请记住,我使用@Html.EditorForModel()
来显示整个表单。
你似乎并不真正理解编辑器模板是如何工作的,或者更重要的是,你似乎并不真正理解你所要求的问题。
首先,你必须考虑到一个下拉列表是两件事。1)项目列表。2)一个属性,它将保存从项目列表中选择的值。
因为EditorTemplate只能应用于模型中的一个属性,你该如何让它应用于两个项目呢?答案是不能,至少在没有某种协议或模式的情况下不能。
你的问题是,你试图将编辑器模板应用到错误的东西在下拉列表是SelectedValue,而不是项目的列表。
你需要做的是像这样:
public class RegisterModel
{
[Required]
[UIHint("DDL")]
[Display(Name = "User Role", Prompt = "User Role")]
public int? RoleId { get; set; } // important that this is nullable
[Required]
[UIHint("DDL")]
[Display(Name = "User Role 2", Prompt = "User Role 2")]
public int? RoleId2 { get; set; } // important that this is nullable
public IEnumerable<SelectListItem> UserRoles { get; set; }
}
在你的控制器中:
public ActionResult Index()
{
var DropDownListModel =
(from mm in db.roles
orderby mm.RoleName
select new SelectListItem { Text = mn.RoleName, Value = mn.RoleId })
.ToList();
return View(new RegisterModel { UserRoles = DropDownListModel });
}
EditorTemplates ' DDL。cshtml
@model int
@Html.DropDownListFor(x => x, ViewBag.DDLItems, ViewBag.DDLUnselectedValue)
然后,在你的视图中,你这样做:
@model RegisterModel
...
<p> A Role ID </p>
@Html.EditorFor(x => x.RoleId,
new { DDLItems = Model.UserRoles, DDLUnselectedValue = "Select A Value..." })
<p> A Second Role ID </p>
@Html.EditorFor(x => x.RoleId2,
new { DDLItems = Model.UserRoles, DDLUnselectedValue = "Select A Value 2..." })
这里需要注意的重要事项是:
因为你正在使用EditorFor的"additionalViewData"字段,这个数据不存在于一般的ViewData/ViewBag字典中,只存在于编辑器模板看到的字典中。因此,您可以将单独的变量传递给Editor Template的每个实例。
使用强类型的DropDownListFor,它将为你省去很多麻烦。使用一个可空的Selected Id字段,这有助于进行验证,并且更容易知道一个值何时没有被选中(如果您检查ModelState, required属性强制设置它)。
将EditorTemplate设置为选定值,而不是下拉列表中的值,因为这是EditorTemplate的"类型"。
不要使用SelectList,只使用SelectListItems集合,效果会好很多。
现在,做了这一切……这真的不会为你节省多少工作……注意,它看起来很像标准的Html helper DropDownListFor…除非你的编辑器模板里面有很多东西//Partial View
@{
List<SelectListItem> DataItems = new List<SelectListItem>();
using(var db = new ModelEntites()){
foreach(var _Item in db.Table.ToList())
{
DataItems.add(_Item.Valriables);
}
ViewBag.PublicName = DataItems;
}
@HtmlDropDownList("PublicName");
}