简体   繁体   中英

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 . Why Matrix is null? What i missed when used @Html.HiddenFor(model => model.Matrix) ?

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

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

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

example 1 :

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

example 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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