What I'm attempting to do is take the data entered into an MVC user form and submit it back to the user in a different view.
I have a private variabled declared in the class:
IList<string> _pagecontent = new List<string>();
Here is my action that accepts the FormCollection object, validates it, and tries to pass it on 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);
}
On the Preview view, I have the following Page Directive, which you can see is correct:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Template.Master" Inherits="System.Web.Mvc.ViewPage<List<string>>" %>
So as you see I'm passing a strongly typed object List. Now I would expect I could use the Model object to get my data, but alas I cannot. At the following line, I get an 'error index out of bounds' exception, stating that the index must be non-negative and less than the size of the collection:
<%if (Model[0].ToString() == "0")
{%>
I also notice that some strange parameters have been added to the url, as it resolves to http://localhost:1894/Admin/Preview?Capacity=4&Count=3
So I have two questions about this.
- When I call RedirectToAction and pass it my List, why is it inaccessible in the view's Model object?
- 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.