简体   繁体   English

ASP.NET MVC视图传递对控制器的空引用

[英]ASP.NET MVC View pass null reference to controller

Why when i click on <input type="submit" value="Transpose" /> it pass TableViewModel object in TransposeMatrix function where TableViewModel.Matrix=null . 为什么当我单击<input type="submit" value="Transpose" />它在TransposeMatrix函数中传递TableViewModel对象,其中TableViewModel.Matrix=null Why Matrix is null? 为什么矩阵为空? What i missed when used @Html.HiddenFor(model => model.Matrix) ? 使用@Html.HiddenFor(model => model.Matrix)时我错过了什么?

TableViewModel: 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 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" />
}

You should do a hidden for every value in the matrix: 您应该对矩阵中的每个值进行隐藏:

@for (int row = 0; row < Model.Rows; row++)
{
    @for (int column = 0; column < Model.Columns; column++)
    {
        @HiddenFor(model => model.Matrix[row, column])
    }
}

Please use below syntax In .cshtml file 请在.cshtml文件中使用以下语法

    **Use FormExtensions.BeginForm Method (HtmlHelper, String, String, Object, FormMethod, Object)**

example 1 : 范例1:

    @using (Html.BeginForm("Index", "Settings",  new { id = "test1" }, FormMethod.Post, null))
    {
      <input type="submit" name="SaveButton" value="Save" />
    }

example 2 :- 范例2:-

@using (Html.BeginForm("TransposeMatrix", "Home", FormMethod.Post, null))
{
    <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" />
    You are using FormExtensions.BeginForm Method (HtmlHelper, String, String, FormMethod, Object), where object is for the HTML attributes to set for the element. Currently it is setting the id of the form tag.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM