【问题标题】:ASP.Net MVC DropDownList EditorTemlateASP.Net MVC 下拉列表编辑器模板
【发布时间】: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


    【解决方案1】:

    您似乎并不真正了解编辑器模板的真正工作原理,或者更重要的是,您似乎并不真正了解您所要求的问题。

    首先,您必须考虑到 DropDownList 是两件事。 1) 物品清单。和 2) 将保存从该项目列表中选择的值的属性。

    因为 EditorTemplate 只能应用于模型中的一个属性,您应该如何使其应用于两个项目?答案是你不能,至少在没有某种协议或模式的情况下不能。

    您的问题是您试图将编辑器模板应用于 DropDownList 中的错误内容是 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 字典中不存在此数据,仅存在于编辑器模板看到的字典中。因此,您可以将单独的变量传递给编辑器模板的每个实例。

    使用强类型的 DropDownListFor,会省去很多麻烦。使用可为空的 Selected Id 字段,这有助于验证并更容易知道何时未选择值(如果您在回发时检查 ModelState.IsValid,则 Requred 属性强制设置它)

    在 Selected 值上设置 EditorTemplate,而不是在 DropDown 值列表上,因为这是 EditorTemplate 的“类型”。

    不要为 SelectList 操心,只需使用 SelectListItems 的集合,效果会好很多。

    现在,已经完成了所有这些.. 它并没有为您节省很多工作... 请注意,它基本上看起来很像标准的 Html 帮助程序 DropDownListFor... 除非您的编辑器模板将包含更多内容它...

    【讨论】:

      【解决方案2】:
      //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");
      }
      

      【讨论】:

      • 所以您想要更多类似于 asp.net 的用户控件,您可以在其中使用填充数据在应用程序中创建下拉列表,然后在其他视图中使用相同的下拉列表?
      • 没有。我可以调用一个编辑器模板来为任何模型或视图模型生成下拉列表。我不希望这在设计时与任何模型相关联。或者这是否意味着我必须为我创建的每种类型的下拉列表创建一个编辑器模板?
      • 我已经更新了我的答案,看看这是否是您正在寻找的更多内容。
      • 如果你把它作为你的局部视图并将 viewbag 和你的公共名称设置为该控件,你将能够用任何东西填充它。
      • 您想了解更多关于渲染局部视图的详细信息吗?
      猜你喜欢
      • 1970-01-01
      • 2011-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 2011-05-14
      • 1970-01-01
      相关资源
      最近更新 更多