簡體   English   中英

ASP.NET MVC視圖傳遞對控制器的空引用

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

為什么當我單擊<input type="submit" value="Transpose" />它在TransposeMatrix函數中傳遞TableViewModel對象,其中TableViewModel.Matrix=null 為什么矩陣為空? 使用@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; }
    }
}

家用控制器:

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

您應該對矩陣中的每個值進行隱藏:

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

請在.cshtml文件中使用以下語法

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

范例1:

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

范例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