1

After return RedirectToAction("Index", emp); i get in public ActionResult Index(Employee emp) List with Count=0; instead of Count=5. Why? 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" />
}

Model and Controller:

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);
        }
    }
}
A191919
  • 3,422
  • 7
  • 49
  • 93

2 Answers2

1

In any case you cannot pass a collection (or a complex object containing a collection) to a GET method using RedirectToAction().

Simple return View or use TempData collection to store the object, and retrieve it in the following action.

return View("Index", emp);

answer Source

Community
  • 1
  • 1
Sain Pradeep
  • 3,119
  • 1
  • 22
  • 31
1

RedirectToAction() method results in a new Get call, that is why your data is lost , you can use a simple Variable Holder or make use of 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");
        }
    }
Kevorkian
  • 430
  • 2
  • 4
  • 14