C# 在ASP.NET MVC中使用Razor和模型元数据生成表单的最佳方法是什么
我已经使用ASP.NETMVC好几个星期了,我非常喜欢它。我花了一些时间来弄清楚如何使用通用的razor模板生成表单字段,使用模型的元数据生成表单字段。感觉MVC更倾向于按模型查看的方法。下面是我到目前为止所做的,但感觉像是一个大的黑客。我使用ajax向WebApi控制器提交数据,但为了清晰起见,我省略了这一点 样本模型C# 在ASP.NET MVC中使用Razor和模型元数据生成表单的最佳方法是什么,c#,asp.net,asp.net-mvc,razor,C#,Asp.net,Asp.net Mvc,Razor,我已经使用ASP.NETMVC好几个星期了,我非常喜欢它。我花了一些时间来弄清楚如何使用通用的razor模板生成表单字段,使用模型的元数据生成表单字段。感觉MVC更倾向于按模型查看的方法。下面是我到目前为止所做的,但感觉像是一个大的黑客。我使用ajax向WebApi控制器提交数据,但为了清晰起见,我省略了这一点 样本模型 public class ClientDto { [Key] [HiddenInput(DisplayValue = true)] public Gui
public class ClientDto
{
[Key]
[HiddenInput(DisplayValue = true)]
public Guid Id { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Email")]
public string Email { get; set; }
[HiddenInput(DisplayValue = true)]
[DataType(DataType.ImageUrl)]
public string ProfilePicUrl { get; set; }
}
public class OrganizationDto
{
[Key]
[HiddenInput(DisplayValue = true)]
public Guid Id { get; set; }
[Required]
[Display(Name = "Organization Name", GroupName = "Company")]
public string Name { get; set; }
[Display(Name = "First Name", GroupName = "Company Admin")]
public string FirstName { get; set; }
[Display(Name = "Last Name", GroupName = "Company Admin")]
public string LastName { get; set; }
[EmailAddress]
[Display(Name = "Email", GroupName = "Company Admin")]
public string Email { get; set; }
[Display(Name = "Address Line 1", ShortName = "Line 1", GroupName = "Address")]
public string AddressLine1 { get; set; }
[Display(Name = "Address Line 2", ShortName = "Line 2", GroupName = "Address")]
public string AddressLine2 { get; set; }
[Display(Name = "Country", GroupName = "Address")]
public string Country { get; set; }
[Display(Name = "City", GroupName = "Address")]
public string City { get; set; }
[Display(Name = "Province", GroupName = "Address")]
public string Province { get; set; }
[Display(Name = "Postal Code", GroupName = "Address")]
public string PostalCode { get; set; }
[Display(Name="Test Bool")]
public bool TestBool { get; set; }
}
剃刀模板
@model object
@{
var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, Model.GetType());
var properties = modelMetadata.Properties.Select(md => new WrappedProperty(md))
.GroupBy(p => p.GroupName);
}
<form class="form-horizontal" id="newClientForm" role="form">
@foreach (var group in properties)
{
if (!string.IsNullOrEmpty(group.Key))
{
@:<fieldset>
<legend>@group.Key</legend>
}
foreach (var property in group)
{
var md = property.Metadata;
if (md.TemplateHint != "HiddenInput")
{
<div class="form-group">
<label class="col-sm-4 control-label" for="@md.PropertyName">@md.DisplayName</label>
<div class="col-sm-8">
@Html.Editor(md.PropertyName)
</div>
</div>
}
else
{
<input class="form-control" id="@md.PropertyName" name="@md.PropertyName" type="hidden" />
}
}
if (!string.IsNullOrEmpty(group.Key))
{
@:</fieldset>
}
}
</form>
@model对象
@{
var modelMetadata=modelmataproviders.Current.GetMetadataForType(null,Model.GetType());
var properties=modelMetadata.properties.Select(md=>newwrappedproperty(md))
.GroupBy(p=>p.GroupName);
}
@foreach(属性中的var组)
{
如果(!string.IsNullOrEmpty(group.Key))
{
@:
@组键
}
foreach(组中的var属性)
{
var md=property.Metadata;
if(md.TemplateHint!=“HiddenInput”)
{
@md.DisplayName
@编辑器(md.PropertyName)
}
其他的
{
}
}
如果(!string.IsNullOrEmpty(group.Key))
{
@:
}
}
在Index.cshtml中,我调用Html.Partial来使用模型对象的实例呈现表单
@using WebApplication1.Models
@{
ViewBag.Title = "Home Page";
}
<br/>
<div class="row">
<div class="col-md-6">
@Html.Partial("_GenericFormTemplate", new OrganizationDto())
</div>
<div class="col-md-6">
@Html.Partial("_GenericFormTemplate", new ClientDto())
</div>
</div>
@使用WebApplication1.Models
@{
ViewBag.Title=“主页”;
}
@Html.Partial(“\u GenericFormTemplate”,新的OrganizationDto())
@Html.Partial(“\u GenericFormTemplate”,new ClientDto())
呈现形式-
MVC是否有从对象数据进行渲染的标准方法?,若要向该注释添加更多信息,请查看“显示和编辑模板”。我仍然需要为每个模型创建一个模板。在上面,我使用了一个模板来呈现所有的输入元素,而不必事先知道模型对象的属性。