MVC - Passing Data Using RedirectToAction ()

I would like to take the data entered into a custom MVC form and display it in a different view.

The class has the following variable:

IList<string> _pagecontent = new List<string>(); 

The following action takes a FormCollection object, validates it, and passes it to the Preview view as a list:

 [Authorize(Roles = "Admins")] [ValidateInput(false)] [AcceptVerbs(HttpVerbs.Post)] public ActionResult UpdateContent(FormCollection collection) { if (ModelState.IsValid) { string PageToInsert = collection["PageToInsert"]; string PageHeader = collection["PageHeader"]; string PageBody = collection["PageBody"]; //validate, excluded... _pagecontent.Add(PageToInsert); _pagecontent.Add(PageHeader); _pagecontent.Add(PageBody); } return RedirectToAction("Preview", _pagecontent); } 

The Preview view has the following directive for passing a strongly typed List object:

 <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Template.Master" Inherits="System.Web.Mvc.ViewPage<List<string>>" %> 

I would expect that I could use the Model object to get my data, but, alas, I can not. In the next line, I get an error index out of bounds exception, indicating that the index must be non-negative and smaller than the size of the collection:

 <% if (Model[0].ToString() == "0") { %> 

And some strange parameters are added to the URL, as it allows http://localhost:1894/Admin/Preview?Capacity=4&Count=3

I have two questions:

  • When I call RedirectToAction and pass it my list, why is it not available in the representation of the model object?
  • What is the correct way to do what I'm trying to do, namely pass a collection of strings in a view to display there?
+57
c # asp.net-mvc redirecttoaction
Mar 23 '09 at 3:27
source share
8 answers

Try using TempData. It looks like a one-shot session object. You put the desired values ​​in TempData, immediately redirect and push them. There is a good entry here: http://blogs.teamb.com/craigstuntz/2009/01/23/37947/

+55
Mar 23 '09 at 3:51
source share

Be careful when using TempData. It works fine in a single server environment, but in a cloud environment it may not work as expected, since you cannot guarantee that the request will go to the same computer. This is because TempData relies on an asp.net session. But if you use another session manager, such as SQL or AppFabric Cache, it will work fine.

+10
Apr 25 '12 at 19:24
source share

The second parameter for RedirectAction is routeValues, not the model.

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

Try using TempData for the model. Its for saving data between redirects.

+7
Mar 23 '09 at 21:43
source share

The problem with RedirectToAction is that it returns HTTP 302 and the browser then goes to it and makes a new HTTP request. You may want to use a cookie and / or session object to store data between requests.

+4
Mar 23 '09 at 3:35
source share

This does not work because RedirectToAction actually sends the Http 302 to the browser. When the browser receives this 302, it makes a new request to the server requesting a new page. New request, new temporary variables.

You will also encounter this problem when trying to save / edit / delete something, and for some reason you deny it, and you need to revert the old form again.

So, instead of:

 return RedirectToAction("Preview", _pagecontent); 

Put the preview logic in a separate method and just call it:

 return PreviewLogic(_pagecontent); 

You can also use TempData [] dic to save data for the next request, as others have said, but then you cannot avoid 302 additional round-trip flights to the server.

+3
Jul 24. '09 at 13:06
source share

It looks like you are trying to do:

 public ActionResult UpdateContent(FormCollection form) { ... return View("Preview", _pagecontent); } 

Please note that redirection should be a "clean list" for the browser (except for things like auth cookie). You cannot tell the browser to transmit information on the next request, since the next request should be able to do it yourself. All you have to do is tell the browser which URL to request next. In ASP.NET MVC, when you pass an object argument to RedirectToAction , the public properties of that object are added as query string parameters to the generated URL.

+2
Mar 23 '09 at 15:33
source share

Can't you just do 2 action results with the same name and mark 1 of them with HttpPost?

  public ActionResult UpdateContent(FormCollection preview = null) { return View(preview); } [HttpPost] public ActionResult UpdateContent(FormCollection collection = null, bool preview = false) { if (preview) return UpdateContent(collection); else return UpdateContent(null); } 
0
May 05 '14 at 17:36
source share

It looks like you are looking for the UpdateModel command:

Post on ScottGu's blog post about:

Improved UpdateModel and TryUpdateModel methods

-3
Mar 23 '09 at 2:59 p.m.
source share



All Articles