Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从javascript调用c#方法进行重定向_C#_Javascript - Fatal编程技术网

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

从javascript调用c#方法进行重定向,c#,javascript,C#,Javascript,我有一个重定向的c#方法 [UIAuthentication()] [UIMethod("cancelredirect", "mth"), UIMethodGroup("employeradmin")] public void CancelRedirect(RequestContext Context) { Context.Redirect(string.Format("View.mth?EmployerID={0}", _employerID)); } 由此,我创建了一个javascri

我有一个重定向的c#方法

[UIAuthentication()]
[UIMethod("cancelredirect", "mth"), UIMethodGroup("employeradmin")]
public void CancelRedirect(RequestContext Context)
{
  Context.Redirect(string.Format("View.mth?EmployerID={0}", _employerID));
}
由此,我创建了一个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();"/>
函数getCancel(){ var confirmCancel=confirm('您确定要取消吗?'); 如果(确认取消) { //重定向到这里 } 其他的 { 返回false; } };
我只是想知道如何从javascript调用c#方法来重定向到page view.mth?我不是100%确定如何做到这一点,因为我从来没有这样做过。感谢您提供的任何帮助

实际上,您的问题应该是:“如何从java脚本调用action方法?”

假设控制器类名为:EmployeeController

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

请通过以下链接了解更多信息:

当您通过ajax调用服务器方法时,您不能执行重定向,因为响应处理程序不是页面

如果调用
CancelRedirect
的目的只是为了获取一个变量,那么服务器端的
EmployerID

您可以通过Ajax获得
EmployerID
,当前端获得
EmployerID
时,只需通过以下方式重定向:

window.location = "/View.mth?EmployerID=xxxx";
Ps:也许您应该将ajax
async
属性设置为
false

试试ajax:

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

为什么不从客户端重定向?Javascript能够重定向该样式链接吗?@willa:这可能会有帮助: