asp.net mvc - Submitting Form in PartialView in jQuery UI Dialog via Ajax only execs success the first time -
I think I'm missing something simple, but I'm blinded by seeing the same code more and more
I have a MVC partial view that includes the form.
& lt; Div class = "clear" & gt; & Lt; / Div & gt; & Lt; Div class = "overflow" & gt; @Using (HTML.biz form (empty, empty, formmath.post, new {id = "formid"})) {& lt; Div class = "errorbox 1" & gt; @html GetData () & lt; / Div & gt; & Lt; Table & gt; @ Html.EditorFor (m = & gt; m.FormModel, "MyEditorTemplate") & lt; Tr & gt; & Lt; Td colspan = "2" & gt; & Lt; / Td> & Lt; Td> & Lt; Input type = "image" src = "@ images. Resol default (" btn-send.jpg ")" alt = "send" class = "send" /> gt; & Lt; / Td> & Lt; / TR & gt; & Lt; / Table & gt; } & Lt; / Div & gt; This view includes some javascript
& lt; Script type = "text / javascript" & gt; $ (Function () ($ ('(form) () (submit (function () {$ .ajax ({cache: false, url: this.action, type: this.method, data: $ (this).) , Success: the function (race) {if (res.success) {warning ("success"); closed DLOG (window); parent special function ();} and {alert ("no success"); $ ('# SomeElement '). Replacewith (res);}}}) Return;})}}; & lt; / script> This is the controlling method that can be executed
[HTTP post] Public Action Rational Index (Mwwimodel View Modell) {if (CheckSuccess ()) {Back Jess No (new {success = true});} return view noayout ("other view", visual model);} This view is loaded in a jQuery.UI dialog. Once the dialog is full, the success function is implemented correctly by clicking the submit button on the form - the information pops up, the dialog stops and the basic function is called. If I popup the dialog then Clicking on the submit button, the call goes to the server and Corrects the process correctly, but reloads the page and displays only JSON string (no warning, etc.). I'm assuming that there is some kind of client-side caching that I'm missing, or is there anything equal to any idea?
Remove document.ready from your view: & lt; Script type = "text / javascript" & gt; $ ('# FormId') Submit (function () {$ .ajax ({cache: false, url: this action, type: this.method, data: $ (this). Serialize (), success: function {Res} {if (res. Success (warning ("success"); closeDialog (); window.parent.specialFunction ();} other {alerts ("no success"); $ ('# someElement'). Html res);}}}); Return false;}); & Lt; / Script & gt; The reason for this is that document.ready has not been executed for the second time when you partially inject your dome. So your submitted event is no longer engaging. First catch it carefully: Deleting document.ready means that you must have this script in the markup for the first time after the form when you first load this page, it does not attach to it at all Will do
Obviously I am saying that this is only to solve your current issue.
But your real problem is that you are mixing markup and HTML. It should never be done. Under the markup sequence, script is related to javascript files separate javascript files so you should separate it into a separate JS file. So the real solution is to subscribe the following script in a different file (or if you are using legacy jQuery versions and if you are using the prehistoric version of jQuery) to subscribe live elements To subscribe, your DOM does not exist yet after subscription: $ (document) .on ('submit', '#formId', function () {$ .ajax ({Cache: incorrect, url: this.action, type: this.method, data: $ (this). Serialize (), success: function (res) {if (res.success) {warning ("Success"); closeDialog (); Window.parent.specialfunction ();} other {alerts ("no success"); $ ('# some element'). Html (res);}}}) Return false; ;});
Comments
Post a Comment