在我的表单中,有些字段似乎没有应用data-val="true"
属性或任何其他data-val
属性(即data-val-required
,data-val-number
,等等。)。前两个字段不起作用,但后面的字段正在工作。
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "PO_Form" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-horizontal">
<hr />
<div class="form-group">
@Html.LabelFor(model => model.PoNumber, "PO #", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PoNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PoNumber, "", new { @class = "text-danger" })
// doesn't work
</div>
</div>
<div class="form-group">
@Html.Label("Customer PO #", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.vendPoNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.vendPoNumber, "", new { @class = "text-danger" })
</div>
// doesn't work
</div>
<div class="form-group">
@Html.Label("Date", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(x => x.poDate, new { @Value = @DateTime.Now.ToShortDateString(), @class = "form-control datepicker" })
@Html.ValidationMessageFor(model => model.poDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.Label("Company", "companyID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div id="partialView">
@{ Html.RenderPartial("_CompanyDropdown", Model); }
</div>
@Html.ValidationMessageFor(model => model.companyID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.salesTax, "salesTax", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.salesTax, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.salesTax, "", new { @class = "text-danger" })
</div>
// works
</div>
<div class="form-group">
@Html.LabelFor(model => model.shipping, "shipping", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.shipping, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.shipping, "", new { @class = "text-danger" })
</div>
// works
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input id="createPO" type="submit" value="Create" class="btn btn-success btn-sm" />
<a href="@Url.Action("Index", "PurchaseOrders")"><span class="btn btn-sm btn-danger">Cancel</span></a>
</div>
</div>
</div>
}
生成实体框架的数据库第一SQL ...
public int id { get; set; }
public string PoNumber { get; set; }
public string vendPoNumber { get; set; }
public System.DateTime poDate { get; set; }
public int companyID { get; set; }
public decimal salesTax { get; set; }
public decimal shipping { get; set; }
我如何使用服务器端验证?
需要多个字段,但从未为EF更新中的这些字段生成[required]
。