我怎样包括一个RedirectToAction的模型? [英] How do I include a model with a RedirectToAction?

查看:93
本文介绍了我怎样包括一个RedirectToAction的模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RedirectToAction 下面,我想传递一个视图模型。我如何模型传递到重定向?

In the RedirectToAction below, I'd like to pass a viewmodel. How do I pass the model to the redirect?

我设置一个断点来检查模型的数值来验证是否正确创建的模型。它是正确的,但所产生的视图不包含在模型属性中找到的值。

I set a breakpoint to check the values of model to verify the model is created correctly. It is correct but the resulting view does not contain the values found in the model properties.

//
// model created up here...
//
return RedirectToAction("actionName", "controllerName", model);

ASP.NET MVC 4 RC

ASP.NET MVC 4 RC

推荐答案

RedirectToAction 返回一个302响应客户端的浏览器,因此浏览器会作出新的GET请求在响应的位置标头值的URL来浏览器。

RedirectToAction returns a 302 response to the client browser and thus the browser will make a new GET request to the url in the location header value of the response came to the browser.

如果你想通过一个简单的精益平面视图模式,第二个动作的方法,你可以使用<一个href=\"https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirecttoaction(v=vs.118).aspx#M:System.Web.Mvc.Controller.RedirectToAction%28System.String,System.String,System.Object%29\">this在 RedirectToAction 方法的重载。

If you are trying to pass a simple lean-flat view model to the second action method, you can use this overload of the RedirectToAction method.

protected internal RedirectToRouteResult RedirectToAction(
    string actionName,
    string controllerName,
    object routeValues
)

RedirectToAction 将(routeValues​​)传递的对象转换为查询字符串,它添加到URL(从第2个参数我们通过生成),并将嵌入得到的URL在响应的地点头。

The RedirectToAction will convert the object passed(routeValues) to a query string and append that to the url(generated from the first 2 parameters we passed) and will embed the resulting url in the location header of the response.

让我们假设您的视图模型是这样的。

Let's assume your view model is like this

public class StoreVm
{
    public int StoreId { get; set; }
    public string Name { get; set; }
    public string Code { set; get; } 
}

和你在你的第一个动作的方法,你可以通过这样的一个对象,像这样

And you in your first action method, you can pass an object of this to the RedirectToAction method like this

var m = new Store { StoreId =101, Name = "Kroger", Code = "KRO"};
return RedirectToAction("Details","Store", m);

这code将发送302响应与位置标头值的浏览器

This code will send a 302 response to the browser with location header value as

Store/Details?StoreId=101&Name=Kroger&Code=KRO

假设你的详细信息操作方法的参数的类型是 StoreVm 中,查询字符串参数值将被正确地映射到参数的属性

Assuming your Details action method's parameter is of type StoreVm, the querystring param values will be properly mapped to the properties of the parameter.

public ActionResult Details(StoreVm model)
{
  // model.Name & model.Id will have values mapped from the request querystring
  // to do  : Return something. 
}

以上将用于传递小的平瘦视图模型工作。但是,如果你想通过一个复杂的对象,你应该尽量遵循PRG模式。

The above will work for passing small flat-lean view model. But if you want to pass a complex object, you should try to follow the PRG pattern.

PRG代表发表 - 重定向 - 获取即可。通过这种方法,你会发出与查询字符串唯一ID的重定向响应,使用的第二GET操作方法可以再次查询的资源,并返回到的东西的看法。

PRG stands for POST - REDIRECT - GET. With this approach, you will issue a redirect response with a unique id in the querystring, using which the second GET action method can query the resource again and return something to the view.

int newStoreId=101;
return RedirectToAction("Details", "Store", new { storeId=newStoreId} );

这将创建网址商店/细节?STOREID = 101
并在您的详细信息 GET 操作,使用STOREID传入,你会得到/从什么地方建 StoreVm 对象(从服务或查询数据库等)

This will create the url Store/Details?storeId=101 and in your Details GET action, using the storeId passed in, you will get/build the StoreVm object from somewhere (from a service or querying the database etc)

public ActionResult Details(string storeId)
{
   // from the storeId value, get the entity/object/resource
   var store = yourRepo.GetStore(storeId);
   if(store!=null)
   {
      // Map the the view model
      var storeVm = new StoreVm { Id=storeId, Name=store.Name,Code=store.Code};
      return View(storeVm);
   }
   return View("StoreNotFound"); // view to render when we get invalid store id
}

的TempData

PRG模式是一个更好的解决方案来处理这个用例。但是,如果你不想做,真想传递遇到一些复杂的数据的无状态HTTP 的请求,你可以使用像的TempData

TempData

Following the PRG pattern is a better solution to handle this use case. But if you don't want to do that and really want to pass some complex data across Stateless HTTP requests, you may use some temporary storage mechanism like TempData

TempData["NewCustomer"] = model;
return RedirectToAction("actionName", "controllerName");

而在你的 GET Action方法看了一遍。

And read it in your GET Action method again.

public ActionResult actionname()
{      
  var model=TempData["NewCustomer"] as Customer
  return View(model);
}

幕后

的TempData 使用会话对象来存储数据。但是,一旦数据被读出的数据将被终止。

TempData uses Session object behind the scene to store the data. But once the data is read the data is terminated.

雷切尔已经写了一个不错的博客<一href=\"http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications\">post讲解时使用TempData的/的ViewData。值得一读。

Rachel has written a nice blog post explaining when to use TempData /ViewData. Worth to read.

这篇关于我怎样包括一个RedirectToAction的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆