<input type="submit" value="Transpose" />
をクリックするとTableViewModel
オブジェクトがTransposeMatrix
関数に渡される理由は、TableViewModel.Matrix=null
です。マトリックスがnullの理由 @Html.HiddenFor(model => model.Matrix)
を使用したときに私が見逃したのは?ASP.NET MVCビューのコントローラへのnull参照の参照
TableViewModel:
namespace MvcApplication1.Models
{
public class TableViewModel
{
public int Rows { get; set; }
public int Columns { get; set; }
public double[,] Matrix { get; set; }
}
}
にHomeController:
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult MatrixSizeIndex()
{
return View();
}
public ActionResult SetMatrixSize(TableViewModel tableViewModel)
{
int rows = tableViewModel.Rows;
int columns = tableViewModel.Columns;
tableViewModel.Matrix = new double[rows, columns];
Random rnd = new Random();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
tableViewModel.Matrix[i, j] = rnd.Next(9);
}
}
return View(tableViewModel);
}
public ActionResult TransposeMatrix(TableViewModel tableViewModel)
{
return View(tableViewModel);
}
}
}
MatrixSizeIndex.cshtml
@model MvcApplication1.Models.TableViewModel
@{
ViewBag.Title = "SetMatrixSize";
}
@using (Html.BeginForm("TransposeMatrix", "Home"))
{
<table>
@for (int row = 0; row < Model.Rows; row++)
{
<tr>
@for (int column = 0; column < Model.Columns; column++)
{
<td>@Model.Matrix[row, column]</td>
}
</tr>
}
</table>
@Html.HiddenFor(model => model.Rows)
@Html.HiddenFor(model => model.Columns)
@Html.HiddenFor(model => model.Matrix)
<input type="submit" value="Transpose" />
}
私が結合して2次元配列を掲示しようとしていません。あなたは "隠された"要素でレンダリングされるものを表示できますか?私はそのバインディングに問題があるか、それが投稿するものがあると思う。 – Theo
私はあなたのコードのテストを試みていませんが、あなたのビューがフラットなものの代わりにienumerableを使用した場合にはうまくいくでしょうか? –
nocturns2