当我创建一个Asp.net Web客户端来测试WCF服务时,WCF服务上的模型类:
public partial class Product
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
[Required]
[StringLength(30)]
public string ProductName { get; set; }
[Column(TypeName = "money")]
public decimal Price { get; set; }
public int Quantity { get; set; }
}
由客户端中的脚手架生成的创建视图是:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Product</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProductName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProductName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProductName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Quantity, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Quantity, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Quantity, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
问题是@html.ValidationMessageFor仅显示Id,Price和Quantity的消息。它不适用于ProductName字段。渲染代码html是:
<input class="form-control text-box single-line" data-val="true" data-val-number="The field Id must be a number."
data-val-required="The Id field is required." id="Id" name="Id" type="number" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="Id" data-valmsg-replace="true"></span>
<input class="form-control text-box single-line" id="ProductName" name="ProductName" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="ProductName" data-valmsg-replace="true"></span>
ProductName字段的输入标记不包括data-val,data-val-required,如id,price和quantity。我该如何解决这个问题?