1

I'm using asp.net mvc2 for my application. I have an ajax request sent using jQuery.

$.ajax{(
url:'/home/index'
type:'post',
data:$('#myform').serialize(),
dataType:'html',
success:function(response)
{
   //update relevant document portion
}
});

Here is my controller method

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
   }
}

In index actionresult Request.IsAjaxRequest() works properly but when its redirected to List actionresult it does not identify it as ajax request. how can I know that list is being called from an ajax redirection?

Edit

Request.IsAjaxRequest is returning true in IE for both index and List methods while in firefox Request.IsAjaxRequest is true only for index method. When I inspect the code for ajax request I could see two of them; first is post to index method and second is Get from List method. IE sends x-requested-with header with both requests while Firefox is sending this header only for first request destined to index method.

How can I make Firefox work like IE (in this scenario only) i.e sending x-requested-with header with both request in case when second request is not originated from client but is a redirection from first request.

1
  • It will not work.Because u r further Redirecting to another action in which it will act as simple redirect.what i want to convey that ur Jquery ajax post will only work for index action.You can do this using a flag variable.just set the flag status with in Request.IsAjaxRequest() under index action and check it in list. Commented Nov 11, 2010 at 12:22

2 Answers 2

1

muhammad,

you should do something like this in your index action:

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  
}

should work as planned i'd think, as long as you had a partialview called List that had a strongly typed class matching the items being passed in.

Sign up to request clarification or add additional context in comments.

4 Comments

hey jim can u please elaborate little bit about "PartialView". I mean how it work basically.Thanks
sure. if you create a partialview with the strongly typed model matching the items, then the partialview will only update the portion of the page targetted by the success: function in your ajax call. you can see an example of this in action by downloading a little app that i created for another question just yesterday. it's here: gatehousemusic.com/downloads/MvcApplication2.zip
if i use return List(id) instead of return redirectToAction, is there anything bad with it. this sure will solve my problem because it will not trigger an other request but rather pull the List method in same request resulting Request.IsAjaxRequest to be true in List actionresult
muhammad - the above approach should take care of any issues and definately 'nothing bad' will happen :)
0

I have done something like

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");
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.