实体框架-数据注释范围验证器引发异常,而不显示错误消息

时间:2018-10-10 04:00:58

标签: asp.net-mvc entity-framework data-annotations

我正在尝试创建有关公司中供应商注册的表格。必填字段之一是“工人人数”。

我在表单模型中添加了范围验证器,如下所示。

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Required(ErrorMessage = "Form Number is required!")]
public int FormNo { get; set; }

[Required(ErrorMessage = "Please fill in the Number of Workers field!")]
[Range(0, 20, ErrorMessage = "Please enter positive number only!")]
public int NWorkers { get; set; }

每次输入超出范围时,它都会引发异常,而不是显示ErrorMessage。

System.Data.Entity.Validation.DbEntityValidationException: 'Validation
failed for one or more entities. See 'EntityValidationErrors' property
for more details.'

如果输入在该范围内,则可以正常工作。

这是我的控制人

public class FormController : Controller {
    private AppDbContext db = new AppDbContext();

    // GET: Form
    public ActionResult Index() {
        return View(db.Forms.ToList());
    }

    [HttpGet]
    public ActionResult Create() {
        return View();
    }

    [HttpPost]
    public ActionResult Create(Form form) {
        form.CreatedAt = DateTime.Now;
        form.UpdatedAt = DateTime.Now;

        db.Forms.Add(form);
        db.SaveChanges();

        return RedirectToAction("index", "form");
    }
}

这是我的观点

<form class="form-horizontal" action="@Url.Action("create", "form")" method="post">
   @if (!ViewData.ModelState.IsValid) {
      <div class="alert alert-danger">
         @Html.ValidationSummary(false, "")
      </div>
   }

   <div class="form-group">
      <label class="col-md-3 control-label" for="FormNo">Form Number</label>
      <div class="col-md-3">
         @Html.TextBox("FormNo", null, new { @class = "form-control required", @required = "true" })
      </div>
      <span class="col-md-3 pull-right note">Note : * is required</span>
   </div>

   <div class="form-group">
      <label class="col-md-3 control-label" for="NWorkers">Number of Workers</label>
      <div class="col-md-3 required">
         @Html.TextBox("NWorkers", null, new { @class = "form-control" })
         <!-- <input type="text" class="form-control" id="NWorkers" name="NWorkers" value="0" /> -->
      </div>
   </div>

   <div class="box-footer">
      <div class="col-md-offset-3">
         <button type="submit" class="btn btn-primary">
            <i class="fa fa-btn fa-floppy-o"></i> Save
         </button>
         <a onclick="history.back();" class="btn btn-warning">
            <i class="fa fa-reply"></i> Cancel
         </a>
      </div>
   </div>
</form>

修改

Error Details

StackTrace

  

”,位于System.Data.Entity.Internal.InternalContext.SaveChanges()\ r \ n
  在System.Data.Entity.Internal.LazyInternalContext.SaveChanges()\ r \ n
  在System.Data.Entity.DbContext.SaveChanges()\ r \ n   中的HSE.Controllers.FormController.Create(Form form)   C:\ Users \ PC17A196 \ source \ repos \ HSE \ HSE \ Controllers \ FormController.cs:line   28 \ r \ n在lambda_method(Closure,ControllerBase,Object [])\ r \ n
  在System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase   控制器,Object []参数)\ r \ n位于   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext   controllerContext,IDictionary 2 parameters)\r\n at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 2   参数)\ r \ n位于   System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult   asyncResult,ActionInvocation innerInvokeState)\ r \ n位于   System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult asyncResult)\r\n at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase 1.End()\ r \ n   在   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult   asyncResult)\ r \ n位于   System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d()\ r \ n   在   System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters。<> c__DisplayClass46.b__3f()“字符串

0 个答案:

没有答案