Jquery Request.IsAjaxRequest在asp.net mvc2中工作不正常
我正在为我的应用程序使用asp.net mvc2。我使用jQuery发送了一个ajax请求Jquery Request.IsAjaxRequest在asp.net mvc2中工作不正常,jquery,asp.net-mvc,ajax,Jquery,Asp.net Mvc,Ajax,我正在为我的应用程序使用asp.net mvc2。我使用jQuery发送了一个ajax请求 $.ajax{( url:'/home/index' type:'post', data:$('#myform').serialize(), dataType:'html', success:function(response) { //update relevant document portion } }); 这是我的控制器方法 public ActionResult index(Book b
$.ajax{(
url:'/home/index'
type:'post',
data:$('#myform').serialize(),
dataType:'html',
success:function(response)
{
//update relevant document portion
}
});
这是我的控制器方法
public ActionResult index(Book book)
{
Repository _repo = new Repository();
_repo.Add(book);
_repo.Save();
if(Request.IsAjaxRequest())
{
return RedirectToAction("List",new{id=book.id});
}
//do something else
}
public ActionResult List(int id)
{
if(Request.IsAjaxRequest())/* here it always returns false even though its been redirected from an ajax request to get here*/
{
//do something
}
}
在索引actionresult Request.IsAjaxRequest()中可以正常工作,但当其重定向到List actionresult时,它不会将其标识为ajax请求。我怎么知道列表是从ajax重定向调用的
编辑
IE中index和List方法的Request.IsAjaxRequest都返回true,而firefox Request.IsAjaxRequest仅对index方法返回true。当我检查ajax请求的代码时,我可以看到其中两个;第一种是post-to-index方法,第二种是Get-from-List方法。IE发送带有两个请求的x-requested-with头,而Firefox只为发送到index方法的第一个请求发送此头
如果第二个请求不是来自客户端,而是来自第一个请求的重定向,我如何使Firefox像IE一样工作(仅在本场景中),即发送带有两个请求头的x-requested-with请求。muhammad
您应该在索引操作中执行以下操作:
public ActionResult index(Book book)
{
Repository _repo = new Repository();
_repo.Add(book);
_repo.Save();
var items = _repo.GetItems(book.id);
if(Request.IsAjaxRequest())
{
return PartialView("List", items);
}
//do something else
}
我认为应该按计划工作,只要您有一个名为List的partialview,它有一个强类型类,与传入的项匹配。muhammad
您应该在索引操作中执行以下操作:
public ActionResult index(Book book)
{
Repository _repo = new Repository();
_repo.Add(book);
_repo.Save();
var items = _repo.GetItems(book.id);
if(Request.IsAjaxRequest())
{
return PartialView("List", items);
}
//do something else
}
我认为应该按计划工作,只要您有一个名为List的partialview,它有一个与传入的项匹配的强类型类
public ActionResult index(Book book)
{
Repository _repo = new Repository();
_repo.Add(book);
_repo.Save();
if(Request.IsAjaxRequest())
{
return List(book.id);
}
//do something else
}
public ActionResult List(int id)
{
if(Request.IsAjaxRequest())/* in this scenario Request.IsAjaxRequest returns true because there is no redirection and no new request*/
{
return View("List");
}
}
我做过类似的事情
public ActionResult index(Book book)
{
Repository _repo = new Repository();
_repo.Add(book);
_repo.Save();
if(Request.IsAjaxRequest())
{
return List(book.id);
}
//do something else
}
public ActionResult List(int id)
{
if(Request.IsAjaxRequest())/* in this scenario Request.IsAjaxRequest returns true because there is no redirection and no new request*/
{
return View("List");
}
}
它将不起作用。因为您将进一步重定向到另一个操作,在该操作中,它将充当简单重定向。我要传达的是,您的Jquery ajax帖子将仅适用于索引操作。您可以使用标志变量来执行此操作。只需将标志状态设置为in-Request.IsAjaxRequest()在index action下,将其签入列表。它将不起作用。因为您将进一步重定向到另一个操作,在该操作中,它将充当简单重定向。我想传达的是,您的Jquery ajax帖子将只对index action起作用。您可以使用一个标志变量来完成此操作。只需使用in-Request.IsAjaxRequest()设置标志状态在索引操作下,在列表中进行检查。嘿,吉姆,你能详细说明一下“PartialView”吗。我是说它基本上是如何工作的。谢谢。如果创建的partialview的强类型模型与项匹配,那么partialview将只更新ajax调用中success:函数所指向的页面部分。通过下载我昨天为另一个问题创建的一个小应用程序,你可以看到一个实际的例子。它就在这里:如果我使用return List(id)而不是return redirectToAction,它有什么不好的地方吗。这肯定会解决我的问题,因为它不会触发另一个请求,而是在相同的请求结果请求中拉列表方法。IsAjaxRequest在List actionresultmuhammad中为true-上述方法应该考虑到任何问题,肯定不会发生“不好的事情”:)嘿,jim,你能详细说明一下吗“局部视图“我是说它基本上是如何工作的。谢谢。如果创建的partialview的强类型模型与项匹配,那么partialview将只更新ajax调用中success:函数所指向的页面部分。通过下载我昨天为另一个问题创建的一个小应用程序,你可以看到一个实际的例子。它就在这里:如果我使用return List(id)而不是return redirectToAction,它有什么不好的地方吗。这肯定会解决我的问题,因为它不会触发其他请求,而是在相同的请求结果请求中拉取List方法。在List actionresultmuhammad中,IsAjaxRequest为true-上述方法应该考虑到任何问题,并且肯定不会发生“不好的情况:)