模型中的MVC地图数组,用于将模型传递给控制器​​

时间:2019-05-05 13:37:03

标签: asp.net-mvc asp.net-mvc-3 razor ajax.beginform

我有几个输入按钮的表单。在此表单中,按钮可以执行操作。用第一个按钮生成表格(属性类型为int [,]的对象)。使用第二个按钮,我将对该对象进行处理。第一个可以,第二个返回null到控制器模型。 如何从带有数组的对象传递给控制器​​?现在在SubmitForm方法中,我具有buttonType的值,但是matrix为null(应该是表中的数据)。感谢任何帮助

型号

public class Matrix
{
    public int[,] Data { get; set; }
}

控制器

  [HttpPost]
   public ActionResult SubmitForm(Matrix matrix, string ButtonType) - matrix is NULL here, how to get info from form
    {
        if (ButtonType == "Rotate")
        {
            return RotateMatrix(matrix);
        }
        //other code
    }

[HttpPost]
public ActionResult RotateMatrix(Matrix matrix)
    {
       //-some code
        return PartialView("_MatrixView", matrix);
    }

查看

@model Project.Models.Matrix

@{
ViewBag.Title = "Home Page";

var options = new AjaxOptions()
{
    UpdateTargetId = "Matrix",
};
}

@using (Ajax.BeginForm("SubmitForm", "Home", FormMethod.Post, options))
{
  <div id="Matrix"></div>
<input type="submit" value="Rotate" name="ButtonType" />
<input type="submit" value="Generate" name="ButtonType"/>
}

PartialView

@model Project.Models.Matrix
<table>
    @for (int column = 0; column < Model.Data.GetLength(0); column++)
    {
        <tr>
            @for (int row = 0; row < Model.Data.GetLength(1); row++)
            {
                var item = Model.Data[column, row];
                <td>@Html.DisplayFor(m => item)</td>
            }
        </tr>
    }
</table>

0 个答案:

没有答案