相关疑难解决方法(0)

MVC - 使用RedirectToAction传递数据()

我想获取在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

所以我有两个问题:

  1. 当我调用RedirectToAction并将其传递给我的List时,为什么在视图的Model对象中无法访问它?
  2. 做正在做的事情的正确方法是什么,即将一组字符串传递给视图以便在那里显示?

c# asp.net-mvc redirecttoaction

59
推荐指数
3
解决办法
7万
查看次数

标签 统计

asp.net-mvc ×1

c# ×1

redirecttoaction ×1