Required field validations not working in JQuery Popup MVC 4

只谈情不闲聊 提交于 2019-11-26 01:24:06

问题


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\');
    });

回答1:


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.



来源:https://stackoverflow.com/questions/31768946/required-field-validations-not-working-in-jquery-popup-mvc-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!