MVC - 传递数据与RedirectToAction() [英] MVC - Passing Data with RedirectToAction()

查看:175
本文介绍了MVC - 传递数据与RedirectToAction()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想借此在一个MVC用户表单输入的数据,并在不同的视图中显示它。

本类具有以下私有变量:

 的IList<串GT; _pagecontent =新的List<串GT;();

下面的行动接受的FormCollection对象,验证它,并将其传递到preVIEW视图列表:

  [授权(角色=管理员)]
[ValidateInput(假)]
的[AcceptVerbs(HttpVerbs.Post)
公众的ActionResult UpdateContent(的FormCollection集合)
{
    如果(ModelState.IsValid)
    {
        字符串PageToInsert =收集[PageToInsert];
        字符串PageHeader =收集[PageHeader];
        字符串PageBody =收集[PageBody];        //验证,排除......        _pagecontent.Add(PageToInsert);
        _pagecontent.Add(PageHeader);
        _pagecontent.Add(PageBody);    }
    返回RedirectToAction(preVIEW,_pagecontent);
}

在preVIEW认为有以下Page指令传递一个强类型的对象列表:

 <%@页标题=LANGUAGE =C#的MasterPageFile =〜/查看/共享/ Template.Master继承=System.Web.Mvc.ViewPage<清单<串GT;>中%GT;

我会希望能够使用的模型对象得到我的数据,但很可惜我不能。在下面的行,我得到一个错误索引越界例外,说明指数必须是非负数且小于集合的大小:

 <若%(型号[0]的ToString()==0){%GT;

和一些奇怪的参数已被添加到URL,因为它解析为
的http://本地主机:1894年/行政/ preVIEW容量= 4和计数= 3

所以,我有两个问题:


  1. 当我打电话RedirectToAction,并将其传递我的名单,为什么无法访问视图的Model对象?

  2. 什么是去这样做我想要做的正确的方式,即通过字符串的集合到一个视图显示在那里?


解决方案

尝试使用TempData的。它就像一个单次会话对象。你把你想成TempData的值,立即重定向并把他们救出来。这里有一个很好的书面记录:<一href=\"http://blogs.teamb.com/craigstuntz/2009/01/23/37947/\">http://blogs.teamb.com/craigstuntz/2009/01/23/37947/

I'd like to take data entered in an MVC user form and display it in a different view.

The class has the following private variable:

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

The following action accepts a FormCollection object, validates it, and passes 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);
}

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

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

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 error index out of bounds exception, stating the index must be non-negative and less than the size of the collection:

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

And 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:

  1. When I call RedirectToAction and pass it my List, why is it inaccessible in the view's Model object?
  2. 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?

解决方案

Try using TempData. It is like a single-shot session object. You put values you want into TempData, immediately redirect and get them out. There is a good writeup here: http://blogs.teamb.com/craigstuntz/2009/01/23/37947/

这篇关于MVC - 传递数据与RedirectToAction()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆