ASP.NET MVC将对象从一个ActionResult传递给另一个

时间:2017-01-04 12:06:46

标签: c# asp.net asp.net-mvc asp.net-mvc-4

return RedirectToAction("Index", emp);之后 我进入public ActionResult Index(Employee emp) Count = 0的列表;而不是Count = 5。为什么? enter image description here

Employee.cs:

namespace MvcApplication2.Models
{
    public class Employee
    {
        public List<List<double>> Machines { get; set; }
    }
}

Index.cshtml:

@model MvcApplication2.Models.Employee

@{
    ViewBag.Title = "Index";
}

<table>
    @foreach (var column in Model.Machines)
    {
        <tr>
            @foreach (var row in column)
            {
                <td>@row</td>

            }
        </tr>
    }
</table>

@using (Html.BeginForm("TransposeMatrix", "Home", FormMethod.Post, null))
{
    for (int i = 0; i < Model.Machines.Count(); i++)
    {
        for (int j = 0; j < Model.Machines[i].Count(); j++)
        {
    @Html.HiddenFor(x => x.Machines[i][j]);
        }
    }
    <input type="submit" value="Transpose" />
}

模型和控制器:

namespace MvcApplication2.Controllers
{
    public class Model
    {
        public List<List<double>> GenerateMatrix(int rows, int columns)
        {
            var matrix = new List<List<double>>();
            Random rnd = new Random();

            for (int i = 0; i < columns; i++)
            {
                List<double> vector = new List<double>();
                for (int j = 0; j < rows; j++)
                {
                    vector.Add(rnd.NextDouble());
                }
                matrix.Add(vector);
            }
            return matrix;
        }
        public List<List<double>> TransposeMatrix(List<List<double>> matrix)
        {
            int columnCount = matrix.Count;
            int rowCount = matrix[0].Count;

            var rowList = Enumerable.Range(0, columnCount)
                                     .Select(x => Enumerable.Range(0, rowCount)
                                                             .Select(y => matrix[y][x])
                                                             .ToList())
                                     .ToList();

            return rowList;
        }
    }
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        Model model = new Model();

        public ActionResult Index(Employee emp)
        {
            if (emp.Machines == null)
            {
                emp.Machines = model.GenerateMatrix(5, 5);
            }
            return View(emp);
        }

        public ActionResult TransposeMatrix(Employee emp)
        {
            emp.Machines = model.TransposeMatrix(emp.Machines);
            return RedirectToAction("Index", emp);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

在任何情况下,您都无法使用RedirectToAction()将集合(或包含集合的复杂对象)传递给GET方法。

简单返回视图或使用 TempData 集合来存储对象,并通过以下操作检索它。

return View("Index", emp);

回答Source

答案 1 :(得分:1)

RedirectToAction()方法会导致新的Get调用,这就是您的数据丢失的原因,您可以使用简单的变量持有者或使用 TempData

public class HomeController : Controller
    {
        //
        // GET: /Home/
        Model model = new Model();
        /* Holder */
        //public static Employee tmpEmp = new Employee();
        public ActionResult Index()
        {
            /* Holder */
            // var emp = tmpEmp; 
            Employee emp = (Employee) TempData["emp"];
            if (emp.Machines == null)
            {
                emp.Machines = model.GenerateMatrix(5, 5);
            }
            return View(emp);
        }

        public ActionResult TransposeMatrix(Employee emp)
        {
            emp.Machines = model.TransposeMatrix(emp.Machines);
            /* Holder */
            // var tmpEmp = emp; 
            TempData["emp"] = emp;
            return RedirectToAction("Index");
        }
    }