开发者

MVC validation with JQuery.Ajax

开发者 https://www.devze.com 2023-02-28 19:34 出处:网络
i am trying to validate a form in a JQuery model dialog that is dynamically loading a view generated from a jquery ajax call

i am trying to validate a form in a JQuery model dialog that is dynamically loading a view generated from a jquery ajax call

there is already one form on the page and this form is being created under the other form, and everything seems to be orking but I am not sure if its possible to use the regular MVC validation on this second form since its being dynamicly created.

here is the HTML view for the main

 <% Html.EnableClientValidation(); %>
    <% using (Html.BeginForm("UpdateFund", "AdministerFundFeature", FormMethod.Post, new { enctype = "multipart/form-data" })) { %>

    <fieldset>

       ...
    <input type="submit" value="Submit" />
   </fieldset>
<%} %>

    <div id="GrantRecipDialog" title="Add/Edit Grant Recipiant">
        <div id="GrantRecipContent"></div>
    </div>

on a button click this function is fired populationg the div with the new form

var url = "<%: Url.Action( "AddOrUpdateGrantRecip", "AdministerFundFeature") %>" + "?aGrantId=" + aGrantId + "&aFundId=" + aFundId;
            $.ajax({
                url: url, 
                success: function (data) {
                    $('#GrantRecipContent').html(data); /*place the data here, and rerender the section*/
                    $('#GrantRecipDialog').dialog("open");
                },
                error: function ()开发者_如何学编程 { alert("There was a problem with your request, please resubmit your request."); },//??
                complete: function() {}
            });

         }

and here is the view that gets rendered in the pop up

<% Html.EnableClientValidation(); %>
<% using (Ajax.BeginForm("updateGrant", "AdministerFundFeature", 
       new AjaxOptions { OnComplete="function(){onGrantRecipUpdate()}", OnFailure="function(){return onGrantFail()}"},
       new { @id = "frmID" }))
   { %>
<fieldset>
   ...

</fieldset>
<%}%>

is it possible to hook the MVC validation up for the pop up, it works fine in the first form, but I am not sure how to explicitly tell MVC to cerate the validation information for the new for generated


Use this after create the form:

$.validator.unobtrusive.parse($('form'));


I came up against this a while ago, and found that I just had to feed my validation errors into a JSON result and deal with them using the complete method of the Ajax form.

I couldn't find the article I linked to, but did find this;

http://www.hightech.ir/SeeSharp/aspnet-validation-with-ajax-and-json

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号