【发布时间】:2014-08-05 23:06:28
【问题描述】:
我创建了一个 DropDownList EditorTemlate,它在我将 ViewBag 传递给 EditorTemlate 时工作正常(我已经将它绑定到视图模型)。下面是我的代码sn-p。 下拉列表:
@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();
}
结束更新
我面临的挑战是它与单个模型相关联。无论如何,我可以让我的 DropDownList EditorTemplate 动态接受模型(通用 DropDownList EditorTemplate,无论项目如何),以便我对所有用户角色列表、产品列表、模块列表......使用相同的下拉列表?我已经被困了2天。在设计时,所有搜索似乎都与模型相关联。
更多更新
假设您有其他想要在同一个视图中呈现的模型,或者其他视图作为下拉列表,则想法是创建一个可以接受模型作为参数的 EditorTemplate,并根据传递给它的模型呈现一个下拉列表。我不想为呈现不同模型的每个视图创建 EditorTemplate。请记住,我使用@Html.EditorForModel() 来显示整个表单。
对不起,伙计,我想我太使用网络表单了,我可以简单地做到这一点。我对 MVC 还是很陌生。
【问题讨论】:
标签: c# asp.net-mvc-4 razorengine