我一直试图将此问题修复几个小时,但似乎无法找出这里的错误。
我有一个视图,其中包含一些我想发布到控制器的字段。现在,我想检查客户端的验证错误。我之前通过添加以下几个项目使用过: - jquery.js(版本1.12.1) - jquery.validate.js(版本1.14.0) - jquery.validate.unobtrusive.js(版本3.2.3) - jquery-unobtrusive-ajax.js(版本3.2.3)
只要我引用jquery.validate.unobtrusive.js,客户端支持验证就可以了。但是当我按下提交按钮时,它就不再提交表单了。它只是不会击中控制器。
我的观点如下:
<div class="row">
<div class="col-md-12">
<div class="portlet light bordered">
<div class="portlet-title">
<div class="caption">
<span class="caption-subject bold uppercase font-dark">Auto toevoegen</span>
</div>
<div class="actions">
@Html.ActionLink("Terug", "Index", null, new { @class = "btn btn-circle green btn-outline btn-sm" })
</div>
</div>
<div class="portlet-body">
@using (Html.BeginForm("Create", "Car", FormMethod.Post, new { @class = "form-horizontal" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.Merk, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Merk, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Merk)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Type, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Type, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Type)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Kenteken, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Kenteken, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Kenteken)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Kilometervergoeding, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Kilometervergoeding, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Kilometervergoeding)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AccountNumber, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AccountNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AccountNumber)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DefaultCar, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DefaultCar)
@Html.ValidationMessageFor(model => model.DefaultCar)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Opslaan" class="btn blue" />
@Html.ActionLink("Annuleren", "Index", null, new { @class = "btn btn-white" })
</div>
</div>
</div>
}
</div>
</div>
</div>
我的控制器操作如下所示:
[HttpPost]
[DisableValidation]
public ActionResult Create(CarViewModel model)
{
if(!ModelState.IsValid)
{
return View(model);
}
try
{
var tblCar = mapper.Map<CarViewModel, tblCar>(model);
_carAppService.Insert(tblCar);
return RedirectToAction("Index");
}
catch(Exception ex)
{
//Add proper error messages
Logger.Debug("Error occured during car adding process: " + ex.InnerException);
//add common container with errors
TempData["Error"] = "Er is iets fout gegaan :(";
return RedirectToAction("Index");
}
}
脚本在启动时加载到一个包中,并按上述顺序加载。现在,只要从bundle config中删除jquery.validate.unobtrusive,就可以使用提交按钮了。然而,客户端支持验证不再有效。
有谁知道这里发生了什么?谢谢!
答案 0 :(得分:0)
好吧我解决了这个问题!
这个答案很可能只有在您使用ASP.NET Boilerplate Zero模板时才有用。
在〜/ Common / Scripts中添加了一个Javascript文件(jquery-validation-custom.js)。
内容与提交按钮混淆。我用我自己版本的模态自定义验证处理程序替换它,现在它可以工作了!