1

I am using .Net 4.0 and a lot of solution currently on the Internet are not working, for example non-exist library.

The whole story is: I have a main view, and within it there is 1 iframe. By clicking on the [submit] button on main view, it will trigger its own POST action as well as I make it trigger the POST of iframe by using javascript as follows:

function TriggerIframeAction() {
    var iframe = document.getElementById("myIframe");
    var iframeForm = iframe.contentWindow.document.getElementById("myIframeForm");
    iframeForm.submit();
}

However I need to make sure main frame's post always runs BEFORE iframe's post, so I am thinking to let main frame's action to trigger that JS.

I know Ajax.BeginForm(...OnSuccess...) may help. However it seems just mean the connection is successfully? If I put my validation code in main frame's action & make ModelState.IsValid = false, "OnSuccess" (so the iframe post) will still be executed which is not what I want ....

Correct me if I am wrong.

1 Answer 1

1
<form id="form1" method="POST" action="/controller/action">

</form>

JS:

$(function(){
    var form = $('#form1'),
        url = form.attr('action'),
        formData = form.serialize();

    form.submit(function(){
        $.post(action, formData, function(result){
            if(result)
                TriggerIframeAction();
        })
        return false;
    }
});

function TriggerIframeAction() {
    var iframe = document.getElementById("myIframe");
    var iframeForm = iframe.contentWindow.document.getElementById("myIframeForm");
    iframeForm.submit();
}

Controller:

public ActionResult Action(FormCollection collection)
{
    // do something
    return Json(true);
}
Sign up to request clarification or add additional context in comments.

1 Comment

sorry for possible errors in code, i have not tested it, but that's the idea how to do it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.