Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC - Passing Data with RedirectToAction()
    text
    copied!<p>I'd like to take data entered in an MVC user form and display it in a different view.</p> <p>The class has the following private variable:</p> <pre><code>IList&lt;string&gt; _pagecontent = new List&lt;string&gt;(); </code></pre> <p>The following action accepts a FormCollection object, validates it, and passes it on to the "Preview" view as a List:</p> <pre><code>[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); } </code></pre> <p>The Preview view has the following Page Directive for passing a strongly typed object List:</p> <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Template.Master" Inherits="System.Web.Mvc.ViewPage&lt;List&lt;string&gt;&gt;" %&gt; </code></pre> <p>I would expect to be able to use the Model object to get my data, but alas I cannot. At the following line, I get an <code>error index out of bounds</code> exception, stating the index must be non-negative and less than the size of the collection:</p> <pre><code>&lt;% if (Model[0].ToString() == "0") { %&gt; </code></pre> <p>And some strange parameters have been added to the URL, as it resolves to <code>http://localhost:1894/Admin/Preview?Capacity=4&amp;Count=3</code></p> <p>So I have two questions:</p> <ol> <li>When I call RedirectToAction and pass it my List, why is it inaccessible in the view's Model object? </li> <li>What is the proper way to go about doing what I'm trying to do, namely pass a collection of strings to a view for display there?</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload