在ASP.NET MVC 4中重定向时传递viewModel

use*_*602 0 asp.net-mvc-4

我有以下声明:

return Redirect(this.Request.UrlReferrer.AbsolutePath);
Run Code Online (Sandbox Code Playgroud)

这会重定向到调用者视图.它工作正常,但现在我需要在重定向时返回一个视图模型,这样的事情(这是错误的):

return Redirect(this.Request.UrlReferrer.AbsolutePath(item));
Run Code Online (Sandbox Code Playgroud)

那我该怎么做呢?

我想这样做是因为我有一个jqrid,其中一个列提供了一些操作,编辑和删除行.因此,如果用户点击编辑,我将从传递给数据库的id中检索一些数据.然后,一旦我得到这些数据,我填充一个视图模型,以便更新视图中的一些文本框,所以我需要在重定向时传递视图模型.

在我的控制器代码下面:

    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      
    }
Run Code Online (Sandbox Code Playgroud)

Sat*_*pal 5

你可以使用TempData喜欢

在你的控制器中

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

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

    //Your Code
}
Run Code Online (Sandbox Code Playgroud)