我正在开发一个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%:

有谁能建议我如何克服这些限制呢?
@Html.DropDownlistFor。但不确定如何在使用@Html.EditorForModel时处理这个问题?其中标签&文本位于同一行,我用一个class=f包装标签,它将以粗体字体显示标签。因此,我是否可以修改EditorForModel生成的输出,使标签和文本框位于同一行,而不是放在两行上?
RESOURCECUSTOMFIELD列表列?发布于 2016-05-06 06:21:42
EditorForModel默认模板不是高度可定制的。你应该写一个EditorTemplate来解决你所有的问题。您可以检查这个教程。您必须手动编写所有属性,但对于每个模型只需要编写一次。将模板放在EditorTemplates/文件夹中,整个应用程序都可以使用它。回答你的子弹:
string类型的默认编辑器是一个文本框( enum类型的下拉列表)。RESOURCECUSTOMFIELD编写编辑器模板,并在模板中使用它。甚至可以为IEnumerable<RESOURCECUSTOMFIELD>编写模板:
@model IEnumerable @foreach(var customModel in Model) { @Html.LabelFor(m => customModel.CUSTOMLABEL ) @Html.TextBoxFor(m => customModel.CUSTOMVALUE )}并使用它(在您的主模板中):
@Html.EditorFor(m => m.RESOURCECUSTOMFIELD )如果您想要更改模型的DisplayFor工作方式,也有一种叫做DisplayFor的东西。
发布于 2016-05-12 05:07:09
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/EditorTemplate中创建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()运行视图页后的结果是

您可以根据需要制作更多自定义模板。
https://stackoverflow.com/questions/36927871
复制相似问题