1

I have below statement:

return Redirect(this.Request.UrlReferrer.AbsolutePath);

this redirects to the caller view. It is working ok, but now I need to return a view model when redirecting, something like this (it's wrong):

return Redirect(this.Request.UrlReferrer.AbsolutePath(item));

So how can I achieve this?

I want to do this because I have a jqrid in which one of its columns offers some actions, edit and delete the row. So if user clicks on edit, i retrieve some data from the id passed to the database. Then once I get this data, I populate a view model in order to update some textboxes in the view, so I need to pass the view model when redirecting.

Below my code in the controller:

    public ActionResult Edit(int id)
    {            
        ItemViewModel item = new ItemViewModel();
        using (DBContext context = new DBContext())
        {
            Items itemToModify = context.Items.Single(i=> i.ItemId == id);

            item.Desc = itemToModify.Desc;
            item.Name = itemToModify.Name;
        }

        return Redirect(this.Request.UrlReferrer.AbsolutePath, item); <-- how to do this      
    }
user304602
  • 991
  • 4
  • 21
  • 39
  • 1
    Why wouldn't you use RedirectToAction("Action", "Controller", new{ paramName = item}); ? Its a bad idea to do html redirects in you controller. – Jared Oct 21 '13 at 13:01
  • Bad idea? well it depends on what you are trying to do. I need to return to a view that is different from that MVC is inferring in the controller. The solution you propose is for redirect to an action within a specific controller, but I do not want that. I want from the controller to return to the same view, i mean, from a view I call to this controller, then after this controller does what it has to do, I need to stay in the same page and update some fields in the view so i need to pass through the view model. – user304602 Oct 21 '13 at 13:55
  • You need an ajax call then – Matt Bodily Oct 21 '13 at 13:58
  • @MattBodily could you put a piece of code? – user304602 Oct 21 '13 at 14:10
  • 2
    You can use `TempData` – Satpal Oct 21 '13 at 14:23
  • 2
    As Satpal, I would advise using TempData, see http://stackoverflow.com/questions/7993263/viewbag-viewdata-and-tempdata – jbl Oct 21 '13 at 14:27
  • using TempData it could be a solution but it is not recommend as it is better to follow the patern view model. Also how to access TempData from my view if i have below: @Html.TextBoxFor(m => m.ItemViewModel.Name) in order to update its value? – user304602 Oct 21 '13 at 14:39
  • Finally what I have done is what Jared said:using RedirectToAction. I have redirected to the action in the correct controller and from there I have pointed to the correct view -View("MyView", ...)-, as I am in the correct controller then.Also To pass data between the actions in different controllers taking into account that my data is not a primitive type, as it is a view model (a complex one) and as It has only two primitive fields, I have passed those two fields as parameters directly.Then,when action receives them I build the view model (populate it with those values) and return it. – user304602 Oct 21 '13 at 15:20
  • Also, instead of passing the primitive types that belongs to the view model, it is possible to use TempData as said here because data in TempData is not lost when using redirecting. – user304602 Oct 21 '13 at 15:23

1 Answers1

5

You can use TempData like

In your controller

public ActionResult Action1()
{
    ItemViewModel item = new ItemViewModel();
    TempData["item"] = item;
    return Redirect("Action2");
}

public ActionResult Action2()
{
    ItemViewModel item = (ItemViewModel)TempData["item"];

    //Your Code
}
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • After the TempData is used, is it good practice to clear it out. dispose? – JoshYates1980 Aug 16 '14 at 19:47
  • @JoshYates1980, anything you put into TempData is discarded after the next request completes. so no need to dispose – Satpal Aug 17 '14 at 07:38
  • Watch the "back button" here. you want want to use "item = TempData["item"] as ItemViewModel.......and if it is null...redirect somewhere......... – granadaCoder Aug 10 '15 at 17:51