【问题标题】:limitations of using @Html.EditorForModel使用 @Html.EditorForModel 的限制
【发布时间】:2016-08-23 23:55:50
【问题描述】:

我正在开发一个 asp.net mvc-5 Web 应用程序。我有以下模型类:-

public class Details4
    {
        [HiddenInput(DisplayValue=false)]
        public string RESOURCENAME { set; get; }
        [Display (Name="Account Name")]
        [Required]
        public string ACCOUNTNAME { set; get; }
        [Display(Name = "Resource type")]
        [Required]
        public string RESOURCETYPE { set; get; }
        [DataType(DataType.Password)]
        [Required]
        public string PASSWORD { set; get; }
        [Display(Name = "Description")]
        [DataType(DataType.MultilineText)]
        public string Description { set; get; }
        [Display(Name= "URL")]
        [Url]
        public string RESOURCEURL { set; get; }
        [Display(Name="Owner Name")]
        [Required]
        public string OWNERNAME { set; get; }
        [Display(Name = "Resource Group Nam")]
        public string RESOURCEGROUPNAME { set; get; }
        [JsonProperty("Domain")]
        public string DomainName { set; get; }
        [JsonProperty("DNSNAME")]
        public string DNSNAME { set; get; }
         [Display(Name = "Department")]
        public string DEPARTMENT { set; get; }
         [Display(Name = "Location")]
        public string LOCATION { set; get; }
        public List<RESOURCECUSTOMFIELD> RESOURCECUSTOMFIELD { set; get; }
    }

 public class RESOURCECUSTOMFIELD
    {
        public string CUSTOMLABEL { set; get; }
        public string CUSTOMVALUE { set; get; }
    }

现在我通常在现场级别使用@Html.EditorFor()LabelFor()。但是对于这个模型,我想开始使用@Html.EditorForModel,因为我在视图上的标记会更少:-

@Html.EditorForModel()

现在结果不是我所期望的 100%:-

任何人都可以建议我如何克服这些限制:-

  1. 有没有办法将 ResourceType 字段作为下拉列表? .现在,如果我要渲染单独的字段,我可以使用@Html.DropDownlistFor .. 但不确定在使用@Html.EditorForModel 时如何处理?

  2. 有没有办法修改生成的布局?,现在在我的应用程序中很常见,我有以下标签布局 --> 文本框:-

    <div>
        <span class="f">@Html.DisplayNameFor(model => model.Resource.RESOURCENAME)</span> 
       @Html.EditorFor(model => model.Resource.RESOURCENAME) 
        @Html.ValidationMessageFor(model => model.Resource.RESOURCENAME)                                              
    </div>
    

我在同一行有标签和文本,我用 class=f 包裹标签,这将以粗体显示标签。那么我可以修改从EditorForModel 生成的输出以使标签和文本框位于同一行,而不是位于 2 个单独的行上吗?

  1. 我能否强制 EditorForModel 呈现 RESOURCECUSTOMFIELD 列表列?

【问题讨论】:

  • EditorForModel() 将为模型中的每种类型使用默认的 EditorTemplates。如果你想要特定的模板,那么你需要创建你自己的EditorTemplates
  • @StephenMuecke 但是如果我使用 EditorForModel() 我怎么能有我的 resourceType 字段的下拉列表?有没有办法强制 EditorForModel 呈现复杂的实体,例如我的情况下的 RESOURCECUSTOMFIELD?
  • 您可以使用[UIHint] 装饰该属性并为该属性创建一个模板并(畏缩)为SelectList 使用ViewBag 属性
  • 就我个人而言,我从不使用EditorForModel(),而我唯一一次使用EditorFor() 是当我有一个复杂类型(单个对象或集合)的EditorTemplate
  • 上帝没有。我使用@Html.TextBoxFor()CheckBoxFor()

标签: css asp.net razor html-helper asp.net-mvc-5


【解决方案1】:

EditorForModel 默认模板不是高度可定制的。你应该写一个EditorTemplate来解决你所有的问题。您可以查看此tutorial。 您必须手动编写所有属性,但您只需为每个模型编写一次。将模板放在文件夹EditorTemplates/ 中,您的整个应用程序都可以使用它。回答你的问题:

  1. 现在您可以编写自己的模板,您可以为您的属性使用任何类型的输入。 string 类型的默认编辑器是 文本框enum 类型为 dropdown)。

  2. 您必须在模板中编写自己的布局。这正是你想要的。

  3. 您还可以为RESOURCECUSTOMFIELD 编写一个编辑器模板,并在您的模板中使用它。您甚至可以为IEnumerable&lt;RESOURCECUSTOMFIELD&gt; 编写模板:

    @model IEnumerable<RESOURCECUSTOMFIELD>
    @foreach(var customModel in Model)
    {
        @Html.LabelFor(m => customModel.CUSTOMLABEL )
        @Html.TextBoxFor(m => customModel.CUSTOMVALUE )
    }
    

并像(在您的主模板中)一样使用它:

   @Html.EditorFor(m => m.RESOURCECUSTOMFIELD )

如果您想更改 DisplayFor 对模型的工作方式,还有一个叫做 DisplayTemplate 的东西。

【讨论】:

    【解决方案2】:

    1.您可以在模型字段上方使用 [UIHint("custom_template")]。 例如:

    public class Class1
        {
            public int id { get; set; }
            public string Name { get; set; }
            public string LastName { get; set; }
    
            [UIHint("gender_template")]
            public string Gender { get; set; }
        }
    

    然后你应该在Shared/EditorTemplates中创建gender_template.cshtml,如下图所示。

    文件gender_template.cshtml中的示例代码。

    @Html.DropDownList(
        "",
        new SelectList(
            new[] 
            { 
                new { Value = "Male", Text = "Male" },
                new { Value = "Female", Text = "Female" },
            },
            "Value",
            "Text",
            Model
        )
    )
    

    然后在View页面代码是

    @model MvcApplication2.Models.Class1 
    @{
        ViewBag.Title = "Index";
    }
    
    
    <h2>Index</h2>
    @Html.EditorForModel()
    

    运行查看页面后的结果是

    您可以根据需要制作更多自定义模板。

    1. 您可以像@html.EditorFor 一样,要使用@Html.DisplayFor 语法,您应该在Shared/DisplayTemplates 中创建模板文件,您可以在模板文件中编写/添加自己的html 语法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多