0

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" />
}
2
  • I have not tried binding and posting a 2D array. Can you show what gets rendered in the "hidden" element? I suspect there is either a problem with that binding or what it posts. Commented Jan 3, 2017 at 15:47
  • I haven't tried testing your code, but would it work if your view used an ienumerable<model> instead of a flat one? Commented Jan 3, 2017 at 16:58

2 Answers 2

2

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])
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.