2017-01-04 3 views
0

の後にreturn RedirectToAction("Index", emp); が入ります。public ActionResult Index(Employee emp) Count with Count = 0; Count = 5の代わりに。どうして? enter image description hereASP.NET MVCは、あるActionResultから別のActionResultにオブジェクトを渡します。

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); 
     } 
    } 
} 

答えて

1

コレクションを渡すことはできませんどのような場合には(またはコレクションを含む複雑なオブジェクト)をRedirectToAction()を使用してGETメソッドに渡します。 。

シンプルリターンビューまたはオブジェクトを格納し、次のアクションでそれを取得するためにTempDataをコレクションを使用します。新しいGetコールで

return View("Index", emp); 

答えSource

1

RedirectToAction()方法の結果は、それはあなたのデータが失われた理由を、あなたは簡単な変数ホルダーを使用するか、を利用することができ

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"); 
     } 
    } 
TempDataを
です
関連する問題