从 MVC 表单返回对象列表 ASP.NET

本文关键字:列表 ASP NET 对象 返回 MVC 表单 | 更新日期: 2023-09-27 18:27:41

我正在使用C#和.NET Framework 4.5.1开发一个 ASP.NET MVC 5应用程序。

我想创建一个动态表单,以允许用户在需要时插入更多字段。然后,当他们提交表单时,在控制器上检索此数据。

这是我的观点:

@model List<TRZF.Web.API.Models.ProductViewModel>
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <link href='http://fonts.googleapis.com/css?family=Lato:100,300,400,700,900' rel='stylesheet' type='text/css'>
    <link href="~/css/common.css" rel="stylesheet" type="text/css" media="all" />
    <link href="~/css/createBatch.css" rel="stylesheet" />
    <link href="~/css/createProduct.css" rel="stylesheet" />
    <link href="~/css/formStyles.css" rel="stylesheet" />
    <link href="~/css/jquery-ui.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-1.11.2.min.js"></script>
    <script src="~/js/createProducts.js"></script>
    <title>Crear</title>
</head>
<body>
    <!----start-header----------->
    <div class="header_bg">
        <div class="wrap">
            <div class="header">
                <!--------start-logo------>
                <div class="logo">
                    <img src="~/images/logo.png" alt="" /><br />
                    <span>Versi&oacute;n 1.1</span>
                </div>  
                <!--------end-logo--------->
                <!----start-nav-------->    
                <div class="nav">
                </div>
                <!-----end-nav-------->
                <div class="clear"> </div>
            </div>
        </div>
    </div>
    <!------end-header------------>
    <div class="slider_bg">
        <div class="wrap">
            <div class="da-slide" style="padding:0%">
                @{
                using (Html.BeginForm("Save", "MyProduct", FormMethod.Post, new { name = "eProductsFrm", id = "eProductsFrm" }))
                {
                    <div>
                        <table class="productsList">
                            <thead>
                                <tr>
                                    <td>
                                        <h4>Producto</h4>
                                    </td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>
                                        <div class="group">
                                            <input type="text" 
                                                class="productClass"
                                                name="model[0].ProductName" 
                                                id="model[0].ProductName"
                                                required />
                                            @*<span class="highlight"></span>
                                            <span class="bar"></span>
                                            <label>Nombre del producto</label>*@
                                        </div>
                                    </td>
                                </tr>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <td>
                                    </td>
                                </tr>
                            </tfoot>
                        </table>
                        <table class="buttonsTable">
                            <tr>
                                <td style="text-align: left;">
                                    <input type="button" id="addrow" value="A&ntilde;adir producto" />
                                </td>
                                <td>
                                    <input type="button" class="ibtnDel"  value="Borrar &uacute;ltimo producto">
                                </td>
                            </tr>
                        </table>
                    </div>
                    <p><input type="button" id="btnSubmit" value="Crear producto(s)" /></p>
                }
                <div>
                    <p>@Html.ActionLink("Inicio", "Index", "Home")</p>
                </div>
                }
            </div>
        </div>
    </div>  
</body>
</html>

问题是当我提交时,在控制器的方法上我得到一个空值。

这是控制器:

public class MyProductController : Controller
{
    //
    public ActionResult Index()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Save(List<ProductViewModel> viewModel)
    {
        return null;
    }
}

当用户单击"提交"按钮时,如何在MyProductController.Save方法上获取某些内容?

从 MVC 表单返回对象列表 ASP.NET

我想你这里有一个错字......它应该如下:

using (Html.BeginForm("Save", "MyProduct", FormMethod.Post, new { name = "eProductsFrm", id = "eProductsFrm" }))
{
           // Your code
}

我已经用这个html代码修复了这个错误:

<td>
    <div class="group">
        <input type="text" 
            class="productClass"
            name="[0].ProductName" 
            id="[0].ProductName"
            required />
        @*<span class="highlight"></span>
        <span class="bar"></span>
        <label>Nombre del producto</label>*@
    </div>
</td>

现在nameid正在[0].ProductName.

更多信息在这里。

为了让 MVC 将您的表单数据绑定到 Action 方法的参数他们的名字应该匹配。

在您的情况下:

...
name="model[0].ProductName" 

应该是:

...
name="viewModel[0].ProductName" 

因为这是您提供的参数的名称:

public ActionResult Save(List<ProductViewModel> viewModel)

还要小心,因为索引不应该被破坏,这意味着它们应该是 0、1、2.. 等,而不会跳过一个数字。

我们在属性中读取的方式是寻找 参数名称[索引]。属性名称。 索引必须从零开始,并且 完整。

您可以在Scott Hanselman的帖子中阅读更多内容 - 这里

您没有将模型属性与 Html 输入正确链接。

错:

<input type="text" class="productClass" name="model[0].ProductName" id="model[0].ProductName" required />

右:

<input type="text" class="productClass" name="@Model[0].ProductName" id="@Model[0].ProductName" required />