问题描述
我有一个MVC 5应用程序,我使用的数据的注释做了大部分的验证。一个在我的类中的属性看起来是这样的:
[必需(的ErrorMessage =请输入企业名称)]
[StringLength(80)]
公共字符串BUSINESSNAME {搞定;组; }
验证工作,但就像我认为应该不会出现在浏览器中发生。在我的网页我有一个保存按钮。如果我离开了公司名称字段为空,然后单击保存,后做是为了看起来,部分控制器方法,如下所示:
[HttpPost]
公众的ActionResult创建(广告商广告客户,收集的FormCollection,HttpPostedFileBase文件)
{
//之前,我们做任何事情,让我们检查,以确保包括已经完成的任何验证是干净的。
如果(!ModelState.IsValid)
{
返回查看(广告客户);
}
...
...
}
当执行此方法时,模型的状态已经被设定为无效。这是好事,因为它是无效的,因为公司名称字段为空。但是,不应该这个验证在客户端发生了什么?
在我的.cshtml文件中的字段如下所示(使用引导程序):
< DIV CLASS =表单组>
@ Html.Label(企业名称,新{@class =控制标签COL-MD-3})
< DIV CLASS =COL-MD-9>
@ Html.TextBoxFor(型号=> model.BusinessName,新{@class =表格控,标题=,自动对焦=真})
@ Html.ValidationMessageFor(型号=> model.BusinessName)
< / DIV>
< / DIV>
我的web.config已正确设置如下:
<&的appSettings GT;
<添加键=网页:版本值=3.0.0.0/>
<添加键=网页:启用VALUE =FALSE/>
<添加键=ClientValidationEnabledVALUE =真/>
<添加键=UnobtrusiveJavaScriptEnabledVALUE =真/>
< /的appSettings>
我发现我的问题。在我的BundleConfig.cs我有以下内容:
bundles.Add(新ScriptBundle(〜/包/ jQuery的)。包括(
〜/脚本/ jquery- {}版本.min.js
〜/脚本/ jQuery的-UI-1.10.4.min.js
〜/脚本/ jquery.base64.js
)); bundles.Add(新ScriptBundle(〜/包/ jqueryval)。包括(
〜/脚本/ jquery.validate.min.js
〜/脚本/ jquery.validate.unobtrusive.min.js
));
不过,我没想到的是,jqueryval束没有得到默认情况下在_Layout.cshtml文件加载。所以,我需要添加,如下所示:
@ Scripts.Render(〜/包/ jQuery的)
@ Scripts.Render(〜/包/ jqueryval)
@ Scripts.Render(〜/包/引导)
@RenderSection(脚本,必需:false)
在我这样做,是因为它的工作应该。当然,这将导致其得到加载的所有网页。这可能不是理想的。如果没有,单独加载它在每一页是必要的。
I have an MVC 5 app, and I'm using data annotations to do a majority of validation. One of the properties in my class looks like this:
[Required(ErrorMessage = "Please enter a business name")]
[StringLength(80)]
public string BusinessName { get; set; }
The validation is working but it doesn't appear to be happening in the browser like I thought it should. On my page I have a Save button. If I leave the Business Name field blank and click Save, a post is done to a controller method that looks, partially, as follows:
[HttpPost]
public ActionResult Create(Advertiser advertiser, FormCollection collection, HttpPostedFileBase file)
{
// Before we do anything, let's check to make sure any validation that's already been done is clean.
if (!ModelState.IsValid)
{
return View(advertiser);
}
...
...
}
When this method is executed, the model state is already set to invalid. That's good because it is invalid because the Business Name field is empty. However, shouldn't this validation be happening in the client?
The field in my .cshtml file looks as follows (using Bootstrap):
<div class="form-group">
@Html.Label("Business Name", new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.TextBoxFor(model => model.BusinessName, new { @class = "form-control", title = "", autofocus = true })
@Html.ValidationMessageFor(model => model.BusinessName)
</div>
</div>
My Web.Config is set correctly as follows:
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
I found my problem. In my BundleConfig.cs I have the following:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.min.js",
"~/Scripts/jquery-ui-1.10.4.min.js",
"~/Scripts/jquery.base64.js"
));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate.min.js",
"~/Scripts/jquery.validate.unobtrusive.min.js"
));
But, what I didn't realize is that the jqueryval bundle DOES NOT get loaded in the _Layout.cshtml file by default. So I needed to add it, as follows:
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
Once I did this, it is working as it should. Of course, this will cause it to get loaded for all pages. That may not be desirable. If not, load it separately in each page as necessary.
这篇关于MVC5 - 数据注释 - 客户端验证没有发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!