我已经放入 [Required(ErrorMessage = "*")]
模型
[Required(ErrorMessage = "*")]
[Display(Name = "Amount")]
public decimal Amount { get; set; }
在 webconfig 中将这些字段设置为 true
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
和
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<tr>
<td>@Html.LabelFor(model => model.Amount)</td>
<td>@Html.EditorFor(model => model.Amount )
@Html.ValidationMessageFor(model => model.Amount )</td>
<input onclick="return validate();" style=" margin:20px; width:120px; height:40px;" type="submit" value="ثبت فیش" />
}
按钮单击不检查验证,将此添加到控制器以进行服务器端验证
if (ModelState.IsValid)
{
obj.DateAdded = DateTime.Now;
obj.OrderID = id;
RingSportEntities objEntity = new RingSportEntities();
objEntity.Payments.Add(obj);
var p = (from o in objEntity.Orders where o.OrderId == id select o).First();
p.Status = 2;
objEntity.SaveChanges();
return View();
}
else { return View(obj); }
现在,它可以在服务器端验证,为什么客户端现在可以工作?