必填字段验证不JQuery的弹出工作MVC 4 [英] Required field validations not working in JQuery Popup MVC 4

查看:280
本文介绍了必填字段验证不JQuery的弹出工作MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JQuery的弹出窗口和我希望把必填字段验证它,为了这个,我已设置模型所需的属性,并在视图中还设置了验证消息对他们来说却是必填字段验证不工作的弹出窗口。必填字段验证正在比JQuery的弹出窗口等形式精细....请指导我,我应该做些什么来解决这个问题...以下是我的code。

示范

  [显示(NAME =材质code)]
[必需(的ErrorMessage =*)]
公共字符串材质code {搞定;组; }

查看

 <立GT;
    @ Html.LabelFor(M = GT; m.Material code)
    @ Html.TextBoxFor(M = GT; m.Material code)
    @ Html.HiddenFor(M = GT; m.Material codeID)
< /李>

和以下是我的鳕鱼埃托奥打开一个jQuery弹出。

  $('#btnAddCharge')。在('点击',功能(事件){
        。事件preventDefault();
        VAR actionURL ='@ Url.Action(编辑,费用,新的{ID = 0,@ticketId = @TicketId,用户Id =用户ID});        $(DialogBox的).dialog({
            的AutoOpen:假的,
            可调整大小:假的,
            标题:编辑,
            模式:真实,
            显示:瞎子,
            宽度:'自动',
            隐藏:盲,
            打开:函数(事件,UI){
                $(本).load(actionURL,功能(HTML){
                    $('形式',HTML).submit(函数(){
                        $阿贾克斯({
                            网址:this.action,
                            类型:this.method,
                            数据:$(本).serialize()
                            成功:函数(RES){
                                如果(res.success){
                                    $(DialogBox的).dialog('接近');
                                }
                            }
                        });
                        返回false;
                    });
                });
            }
        });        $(DialogBox的).dialog('开');
    });


解决方案

在最初加载页面的验证器解析。当您添加动态内容,你需要重新分析验证。修改你的脚本包含以下几行内容加载后

  $(本).load(actionURL,功能(HTML){
    //重新分析验证
    变种形式= $('形式');
    form.data('验证',NULL);
    $ .validator.unobtrusive.parse(表);
    $('形式',HTML).submit(函数(){
        ....

旁注:你已经表明不包括 @ Html.ValidationMessageFor(M = GT; m.Material $ C $三)code 但我相信这是包括在内。

I have JQuery popups and i want to put required field validations on it and for this i have set required attributes in model and have also set the validation message for them in the view but that required field validations are not working on popups. Required field validation is working fine on forms other than JQuery Popups....Please guide me that what should i do to tackle this issue...Following is my code.

Model

[Display(Name = "Material Code")]
[Required(ErrorMessage = "*")]
public string MaterialCode { get; set; }

View

<li>
    @Html.LabelFor(m => m.MaterialCode)
    @Html.TextBoxFor(m => m.MaterialCode)
    @Html.HiddenFor(m => m.MaterialCodeId)
</li>

and following is my cod eto open a JQuery popup.

$('#btnAddCharge').on('click', function (event) {  
        event.preventDefault();
        var actionURL = '@Url.Action("Edit", "Charges", new { Id = 0, @ticketId = @TicketId, UserId = UserId })';

        $(dialogBox).dialog({
            autoOpen: false,
            resizable: false,
            title: 'Edit',
            modal: true,
            show: "blind",
            width: 'auto',
            hide: "blind",
            open: function (event, ui) {
                $(this).load(actionURL, function (html) {
                    $('form', html).submit(function () {
                        $.ajax({
                            url: this.action,
                            type: this.method,
                            data: $(this).serialize(),
                            success: function (res) {
                                if (res.success) {
                                    $(dialogBox).dialog('close');
                                }
                            }
                        });
                        return false;
                    });
                });
            }
        });

        $(dialogBox).dialog('open');
    });

解决方案

The validator is parsed when the page is initially loaded. When you add dynamic content you need to reparse the validator. Modify your script to include the following lines after the content is loaded

$(this).load(actionURL, function (html) {
    // Reparse the validator
    var form = $('form');
    form.data('validator', null);
    $.validator.unobtrusive.parse(form);
    $('form', html).submit(function () {
        ....

Side note: The code you have shown does not include @Html.ValidationMessageFor(m => m.MaterialCode) but I assume this is included.

这篇关于必填字段验证不JQuery的弹出工作MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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