[RESOLVED]Ajax return a string from a partial and adding unobtrusive validation question

Hi

I have a question, I am having a problem with an ajax call, basically there is a problem becuase it calls the database twice and I get an error.

I am thinking a work around would be to build a partial, change it to a string and send it to the ajax call, before I append it I will do a replace to change the values as needed and then append it, howewer my question is can I retain the unobtrusive validation
in doing this?

I will be adding

$("form").removeData("validator");
$("form").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse("form");

But will it work?

I would check something else first.

You say your ajax call gets fired twice.

That means you’re loading the unobtrusive script twice so check your main view and your partial. That’s a sign both add that particular script.

First check your loaded script and if you notice anything doubled up then remove one. This will fix your issue.

Hi,

First, please refer to eidand’s reply.

Secondly, to load partial view, you could load it directly.

$.get(url, function(data) {
        $("#your div id").html(data);
    });

On the other hand, your code is ok.

There is a link that may benefit you:

# Unobtrusive validation – Partial View

http://www.mfranc.com/javascript/unobtrusive-validation-in-partial-views/

Best Regards

Starain

Thanks for the info, my JS isn’t being fired twice and could be optimizedto work better, but my question is if I get a partial and turn it to a string, can I still add unobtrusive validation? or is there a way to do a replace in JS to replace some characters
in the partial so I can keep my mapping?

Eg I want to return a string or partial > change characters to keep mapping > append into a div > maintain unobtrusive validation

Hi,

EnenDaveyBoy

can I still add unobtrusive validation?

Yes.

EnenDaveyBoy

is there a way to do a replace in JS to replace some characters in the partial so I can keep my mapping?

After load the data to the current page (e.g. in div), you could handle them directly, the way is the same as other part. The mapping is based on the name of element.

Best Regards

Starain

Does this still apply if I return a string and not a partial?

Hi,

EnenDaveyBoy

Does this still apply if I return a string and not a partial?

It doesn’t related to whether the action return the string or PartialViewResult. After add it to the page, they are the same.

Best Regards

Starain

Leave a Reply