我想获取在MVC用户表单中输入的数据并将其显示在不同的视图中.
该类具有以下私有变量:
IList<string> _pagecontent = new List<string>();
Run Code Online (Sandbox Code Playgroud)
以下操作接受FormCollection对象,验证它,并将其作为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);
}
Run Code Online (Sandbox Code Playgroud)
预览视图具有以下页面指令,用于传递强类型对象List:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Template.Master" Inherits="System.Web.Mvc.ViewPage<List<string>>" %>
Run Code Online (Sandbox Code Playgroud)
我希望能够使用Model对象来获取我的数据,但我不能.在接下来的行中,我得到一个error index out of bounds异常,声明索引必须是非负数且小于集合的大小:
<% if (Model[0].ToString() == "0") { %>
Run Code Online (Sandbox Code Playgroud)
并且一些奇怪的参数已经添加到URL中,因为它解析为
http://localhost:1894/Admin/Preview?Capacity=4&Count=3
所以我有两个问题: