简体   繁体   English

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

[英]Return list of objects from ASP.NET MVC form

I'm developing an ASP.NET MVC 5 app with C# and .NET Framework 4.5.1. 我正在使用C#和.NET Framework 4.5.1开发ASP.NET MVC 5应用程序。

I want to create a dynamic form to allow users insert more fields if they need more. 我想创建一个动态表单,以允许用户在需要更多字段时插入更多字段。 And then, when they submit the form retrieve this data on Controller. 然后,当他们提交表单时,在Controller上检索此数据。

This is my view: 这是我的看法:

@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>

The problem is that when I do submit, on Controller's method I get a null. 问题是,当我提交时,在Controller的方法上,我得到一个空值。

This is the controller: 这是控制器:

public class MyProductController : Controller
{
    //
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Save(List<ProductViewModel> viewModel)
    {
        return null;
    }
}

How can I do to get something on MyProductController.Save method when the user click on Submit button? 用户单击“提交”按钮时,如何在MyProductController.Save方法上获取内容?

I guess you have a typo here.. it should be as below: 我想您在这里有错别字。

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

I have fixed this error with this html 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>

Now name and id are [0].ProductName . 现在, nameid[0].ProductName

More info here . 更多信息在这里

In order for MVC to bind your form data to the Action method's parameters their names should match. 为了使MVC将表单数据绑定到Action方法的参数,它们的名称应该匹配。

In your case: 在您的情况下:

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

Should be: 应该:

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

Because this is the name of the parameter that you have provided: 因为这是您提供的参数的名称:

public ActionResult Save(List<ProductViewModel> viewModel)

Also be careful because the indexes should not be broken, which means that they should be 0, 1, 2.. etc. without skipping a number. 也要小心,因为索引不应折断,这意味着它们应为0、1、2 ..等,而不跳过数字。

The way that we read in the properties is by looking for parameterName[index].PropertyName. 我们在属性中读取的方式是通过查找parameterName [index] .PropertyName。 The index must be zero-based and unbroken. 索引必须从零开始且不间断。

You can read more in Scott Hanselman's post - here 您可以在Scott Hanselman的帖子中了解更多信息- 在这里

You are not linking model properties with your Html input properly. 您没有将模型属性与HTML输入正确链接。

Wrong: 错误:

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

Right: 对:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM