Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.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
如何停止MVC RedirectToRouteResult JavaScript?_Javascript_Jquery_Asp.net Mvc - Fatal编程技术网

如何停止MVC RedirectToRouteResult JavaScript?

如何停止MVC RedirectToRouteResult JavaScript?,javascript,jquery,asp.net-mvc,Javascript,Jquery,Asp.net Mvc,我正在设计一个网站,并试图与关闭或打开的javascript兼容 我有一个控制器操作,名称如下 public RedirectToRouteResult AddWorkAssignment(int id) { // delegate the work to add the work assignment... return RedirectToAction("Index", "Assignment"); } 而我的jQuery我做了一个帖子 $('#someButton').

我正在设计一个网站,并试图与关闭或打开的javascript兼容

我有一个控制器操作,名称如下

public RedirectToRouteResult AddWorkAssignment(int id)
{
    // delegate the work to add the work assignment...

    return RedirectToAction("Index", "Assignment");
}
而我的jQuery我做了一个帖子

$('#someButton').click(function() {
    var id = $('#whereTheIDIs').val();
    $.post('/Assignment/AddWorkAssignment/' + id);
    return false;
});

但是控制器上的RedirectToAction就可以做到这一点。。。。如何停止重定向,或者如何构造控制器和页面以处理此问题,因为我希望在关闭javascript时发生重定向。

将控制器更改为类似以下内容:

public ActionResult AddWorkAssignment(int id)
{
    // do work to add the work assignment....

    if (Request.IsAjaxRequest())
        return Json(true);

    return RedirectToAction("Index", "Assignment");
}
您也可以创建自己的过滤器属性。。。很像AcceptVerbs属性

HTHs
查尔斯

编辑:AjaxRequest操作方法SelectorAttribute属性

从开始

然后您的控制器:

public RedirectToRouteResult AddWorkAssignment(int id)
{
    // do work to add the work assignment....

    return RedirectToAction("Index", "Assignment");
}

[AjaxRequest]
public JsonResult AddWorkAssignment(int id)
{
    // do work to add the work assignment....

    return Json(true);
}
public RedirectToRouteResult AddWorkAssignment(int id)
{
    // do work to add the work assignment....

    return RedirectToAction("Index", "Assignment");
}

[AjaxRequest]
public JsonResult AddWorkAssignment(int id)
{
    // do work to add the work assignment....

    return Json(true);
}