为什么我的MVC4视图中没有进行验证检查? [英] Why validation check is not happening in my MVC4 view?

查看:84
本文介绍了为什么我的MVC4视图中没有进行验证检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在使用MVC网站项目。我在我的视图中编写了用于验证某些控件的代码。但是在提交表单时,不会显示这些消息。如果输入所有字段,甚至不会转到控制器方法。



我视图中的代码

Hi All,

I am working with a MVC website project. I have wrote codes for validating certain controls in my view. But while submitting the form these messages are not displayed. and not even go to the controller method if all the fields are entered.

Code in my View

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true);
    <fieldset style="margin: 20px">
        <legend>Contact Us</legend>
        <div class="editor-label">
            <label for="unCandidateFirstName">
                Name</label>
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.unCandidateFirstName)
            @Html.ValidationMessageFor(model => model.unCandidateFirstName)
        </div>
        <div class="editor-label">
            <label for="unCandMobilePhone">
                Mobile No</label>
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.unCandMobilePhone)
            @Html.ValidationMessageFor(model => model.unCandMobilePhone)
        </div>        
        <p>
            <input type="submit" value="Contact Us" />
        </p>
    </fieldset>                                                
}





请任何人帮助我..



Please any one help me..

推荐答案

确保完成以下操作



为了启用客户端验证,我们需要包含jQuery min,validate&我们的视图或布局页面中的不显眼的脚本按以下顺序

Make sure that you are done the following things

For enabling client side validation, we required to include the jQuery min, validate & unobtrusive scripts in our view or layout page in the following order
<script src="@Url.Content("~/Scripts/jquery-1.6.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>





我们可以通过设置值来启用和禁用客户端验证ClientValidationEnabled& UnobtrusiveJavaScriptEnabled键为true或false。此设置将应用于应用程序级别。



We can enable and disable the client-side validation by setting the values of ClientValidationEnabled & UnobtrusiveJavaScriptEnabled keys true or false. This setting will be applied to application level.

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


是的我同意Jameel的请求检查引用并检查您在视图模型中使用的模型验证属性。

如果需要,则为必需属性/正则表达式。根据输入,您还可以尝试应用自定义验证消息。

Yes I agree with Jameel, please do check for the references and also check for the model validation attribute you have used in the view model or not.
Like Required attribute/regular expressions if any. And based on the input, you can also try and apply custom validation message.
ModelState.AddModelError("MODEL PROPERTY","MESSAGE");
return View(model);





希望这会有所帮助。

如果有的话,回复你的查询。

谢谢。



Hope this helps.
Post back your queries if any.
Thanks.


这篇关于为什么我的MVC4视图中没有进行验证检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆