ASP.NET MVC View将null引用传递给控制器

时间:2017-01-03 15:38:19

标签: c# asp.net asp.net-mvc asp.net-mvc-4

为什么当我点击<input type="submit" value="Transpose" />时,它会TableViewModel TransposeMatrix函数TableViewModel.Matrix=null传递@Html.HiddenFor(model => model.Matrix)对象。为什么Matrix是null? 使用namespace MvcApplication1.Models { public class TableViewModel { public int Rows { get; set; } public int Columns { get; set; } public double[,] Matrix { get; set; } } } 时我错过了什么?

TableViewModel:

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

HomeController中:

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

MatrixSizeIndex.cshtml

bos-mp9ps:~ xyz$ alias ssh
alias ssh='ssh -A xyz@bos-lpaw1'

bos-mp9ps:~ xyz$ ssh -A xyz@bos-lpaw1
bash: xyz@bos-lpaw1: command not found

bos-mp9ps:~ xyz$ ssh
Welcome to Ubuntu 14.04.3 LTS (GNU/Linux 3.13.0-65-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

429 packages can be updated.
0 updates are security updates.

New release '16.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

You have new mail.
Last login: Tue Jan  3 10:29:27 2017 from 172.19.37.47
xyz@bos-lpaw1:~$ 

2 个答案:

答案 0 :(得分:2)

您应该为矩阵中的每个值执行隐藏:

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

答案 1 :(得分:1)

请在.cshtml文件中使用以下语法

dataset1 <- structure(list(Date = structure(1:3, .Label = c("7/11/2013 18:33",
  "7/11/2013 18:34", "7/11/2013 18:35"), class = "factor"), ozone = c(40.7, 40.4,
  40.9)), .Names = c("Date", "ozone"), row.names = c(NA, 3L), class = "data.frame")
#convert dataframe to xts object
library(xts)
xt1 <- xts(dataset1[,-1],
           order.by = as.POSIXct(dataset1$Date, format= "%m/%d/%Y %H:%M"))
x_updated <- apply.daily(xt1, colMeans)
#convert back the dataset to data.frame
write.csv(as.data.frame(x_updated), file="daily")

示例1:

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

示例2: -

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