简体   繁体   English

从javascript调用c#方法进行重定向

[英]Call c# method from javascript for a redirect

I have ac# method for a redirect 我有一个重定向的ac#方法

[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. 由此,我制作了一个javascript函数,如果按下表单上的取消按钮,则会调用该函数。

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? 我只是想知道您将如何从javascript调用c#方法来重定向到页面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?" 实际上,您的问题应该是:“如何从Java脚本调用动作方法?”

Assuming your controller class name: EmployeeController 假设您的控制器类名称为:EmployeeController

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

Please go through following link for more info: How to redirect to another webpage in JavaScript/jQuery? 请通过以下链接获取更多信息: 如何在JavaScript / jQuery中重定向到另一个网页?

when you call server method by ajax, you can't do Redirect ,because the response handler is not the page. 当您通过ajax调用服务器方法时,您不能执行重定向,因为响应处理程序不是页面。

If the purpose you call CancelRedirect just only to get one variable , the EmployerID from server side. 如果您调用CancelRedirect的目的仅是从服务器端获取一个变量EmployerID

you can get EmployerID by Ajax and when the front side get the EmployerID , just redirect by : 您可以通过Ajax获取EmployerID ,当前端获得EmployerID ,只需通过以下方式重定向:

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

Ps: maybe you should make the ajax async property to false ps:也许您应该将ajax async属性设置为false

try with ajax : 尝试使用ajax:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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