[英]Form not posting when using unobtrusive validation
I've been trying to get this problem fixed for several hours now, but can't seem to find out what's wrong here. 我一直在尝试解决此问题已有几个小时,但似乎无法找出问题所在。
I have a view with a few fields that I want to post to my controller. 我有一个视图,其中包含一些要发布到控制器的字段。 Now, I want to check for validation errors on the client side.
现在,我想在客户端检查验证错误。 I've used this in several projects before by adding: - jquery.js (version 1.12.1) - jquery.validate.js (version 1.14.0) - jquery.validate.unobtrusive.js (version 3.2.3) - jquery-unobtrusive-ajax.js (version 3.2.3)
在添加之前,我已经在多个项目中使用了此功能:-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)
As soon as a I reference jquery.validate.unobtrusive.js, the client sided validation works. 一旦我引用了jquery.validate.unobtrusive.js,客户端验证就会生效。 But when I press the submit button, it just won't submit the form anymore.
但是,当我按下“提交”按钮时,它将不再提交表单。 It just won't hit the controller.
它只是不会击中控制器。
My view is as following: 我的看法如下:
<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>
My controller action looks like this: 我的控制器动作如下所示:
[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");
}
}
The scripts are loading in a bundle on startup and are loading in the order stated above. 这些脚本在启动时以捆绑方式加载,并按照上述顺序加载。 Now as soon as I remove the jquery.validate.unobtrusive from my bundle config, the submit button works.
现在,一旦我从捆绑包配置中删除了jquery.validate.unobtrusive,“提交”按钮就会起作用。 The client sided validation doesn't work anymore then though.
但是,客户端验证不再起作用。
Does any know what's going on here? 有谁知道这是怎么回事吗? Thanks!
谢谢!
Alright I fixed the problem! 好吧,我解决了这个问题!
This answer will most likely only be useful is you make use of ASP.NET Boilerplate Zero template. 仅当您使用ASP.NET Boilerplate Zero模板时,此答案才可能很有用。
In ~/Common/Scripts a Javascript file was added (jquery-validation-custom.js). 在〜/ Common / Scripts中添加了一个Javascript文件(jquery-validation-custom.js)。
The content messed with the submit button. 内容被提交按钮弄乱了。 I replaced it with my own version of a custom validation handler for modals and now it works!
我用自己的模态自定义验证处理程序版本替换了它,现在它可以工作了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.