简体   繁体   中英

Call c# method from javascript for a redirect

I have ac# method for a redirect

[UIAuthentication()]
[UIMethod("cancelredirect", "mth"), UIMethodGroup("employeradmin")]
public void CancelRedirect(RequestContext Context)
{
  Context.Redirect(string.Format("View.mth?EmployerID={0}", _employerID));
}

From this I have made a javascript function which is called if the cancel button on a form is pressed.

function getCancel() {
    var confirmCancel = confirm('Are you sure you want to cancel?');
    if(confirmCancel)
    {
      //redirect here
    }
    else
    {
    return false;
    }
    };

<input type="submit" id="Cancel" value="Cancel" name="Cancel" class="input_button" onclick="return getCancel();"/>

I was just wondering how would you call the c# method from the javascript to redirect to the page view.mth? I am not 100% sure how to do this as I have never done one one these before. Thanks for any help which you can give

Actually your question should be: "How to call action method from java-script?"

Assuming your controller class name: EmployeeController

if(confirmCancel)
{
  document.location = = '/Employee/CancelRedirect';
}

Please go through following link for more info: How to redirect to another webpage in JavaScript/jQuery?

when you call server method by ajax, you can't do Redirect ,because the response handler is not the page.

If the purpose you call CancelRedirect just only to get one variable , the EmployerID from server side.

you can get EmployerID by Ajax and when the front side get the EmployerID , just redirect by :

window.location = "/View.mth?EmployerID=xxxx";

Ps: maybe you should make the ajax async property to false

try with ajax :

if(confirmCancel)
{
var url = '/Employee/CancelRedirect';
            $.ajax({
                url: url,
                type: 'POST',
                cache: false,
                data :{ valeur : if you want to send a value }
            })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM