简体   繁体   English

MVC3项目中的jQuery验证

[英]jQuery Validation in MVC3 Project

When I create a new MVC3 application, add a model, add a [Required] attribute to the model, scaffold a new edit view for this model, and add a @Html.ValidationMessageFor(...) , everything works beautifully. 当我创建一个新的MVC3应用程序时,添加一个模型,向该模型添加[Required]属性,为该模型添加一个新的编辑视图,并添加一个@Html.ValidationMessageFor(...) ,一切正常。 If I attempt to submit the edit form without filling in a value it gives me a validation message. 如果我尝试提交编辑表单时未填写值,则会给我一条确认消息。

My problem is that this is not working in the enterprise app I'm working on. 我的问题是,这在我正在使用的企业应用程序中不起作用。 Here's what I've done and I wonder if you can think of anything else I can check to troubleshoot. 这是我所做的,我想知道您是否可以想到其他我可以检查以解决问题的方法。 I can't share the code, but perhaps you can see an obvious omission from the list... 我无法共享代码,但是也许您可以从列表中看到明显的遗漏...

  • Add [Required] attribute to my model's property [Required]属性添加到模型的属性中
  • Make sure jquery.validate.js and jquery.validate.unobtrusive.js are referenced 确保引用了jquery.validate.js和jquery.validate.unobtrusive.js
  • Added Html.EnableClientValidation(true) and Html.EnableUnobtrusiveJavaScript(true) on my view 在我的视图中添加了Html.EnableClientValidation(true)Html.EnableUnobtrusiveJavaScript(true)
  • Added my @Html.ValidationMessageFor(...) next to my control (which is a @Html.TextBoxFor ) 在我的控件(这是@Html.TextBoxFor )旁边添加了我的@Html.ValidationMessageFor(...)

The issue is that I can submit the form and no validation is performed. 问题是我可以提交表单,并且不执行任何验证。 I get a server-side error stating that the entity is not valid, which I would expect if it gets past the client-side validation. 我收到一个服务器端错误,指出该实体无效,如果它通过了客户端验证,我将期望该错误。

When I inspect the source, it has not rendered the validation attributes to my textbox. 当我检查源代码时,它没有将验证属性呈现给我的文本框。 I would expect it to look something like this... 我希望它看起来像这样...

<input class="text-box single-line" data-val="true" 
    data-val-required="The Property1 field is required." 
    id="Property1" name="Property1" type="text" value="" />

But instead it looks more like this... 但是相反,它看起来更像这样……

<input id="Reminder_Description" name="Reminder.Description" 
    style="width:300px;" type="text" value="" />

What exactly is responsible for injecting those attributes? 注入这些属性的确切原因是什么?

Thanks! 谢谢!

I figured out the issue. 我发现了问题。 I just didn't know that attributes are not passed through a service layer (WCF), so the Required attribute was not effective. 我只是不知道属性没有通过服务层(WCF)传递,因此Required属性无效。 To remedy, we added a local reference to the entities assembly (which makes sense in our architecture). 为了解决这个问题,我们向实体程序集添加了本地引用(这在我们的体系结构中很有意义)。 Thanks. 谢谢。

Have you checked your web.config for: 您是否检查过web.config中的以下内容:

<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM