C# 从ASP.NET MVC表单返回对象列表
我正在用C#和.NET Framework 4.5.1开发一个ASP.NET MVC 5应用程序 我想创建一个动态表单,允许用户在需要更多字段时插入更多字段。然后,当他们提交表单时,在控制器上检索这些数据 我的看法是:C# 从ASP.NET MVC表单返回对象列表,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我正在用C#和.NET Framework 4.5.1开发一个ASP.NET MVC 5应用程序 我想创建一个动态表单,允许用户在需要更多字段时插入更多字段。然后,当他们提交表单时,在控制器上检索这些数据 我的看法是: @model List<TRZF.Web.API.Models.ProductViewModel> @{ Layout = null; } <!DOCTYPE html> <html> <head> <met
@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ó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ñadir producto" />
</td>
<td>
<input type="button" class="ibtnDel" value="Borrar ú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>
@型号列表
@{
布局=空;
}
克雷尔
版本ó;n 1.1
@{
使用(Html.BeginForm(“Save”,“MyProduct”,FormMethod.Post,new{name=“eProductsFrm”,id=“eProductsFrm”}))
{
产品
@*
产品名称*@
}
@ActionLink(“Inicio”、“索引”、“主页”)
}
问题是当我提交时,在控制器的方法上我得到一个空值
这是控制器:
public class MyProductController : Controller
{
//
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Save(List<ProductViewModel> viewModel)
{
return null;
}
}
公共类MyProductController:控制器
{
//
公共行动结果索引()
{
返回视图();
}
[HttpPost]
公共操作结果保存(列表视图模型)
{
返回null;
}
}
当用户单击“提交”按钮时,如何在MyProductController.Save方法上获取内容?我猜您在这里输入了错误。。应如下所示:
using (Html.BeginForm("Save", "MyProduct", FormMethod.Post, new { name = "eProductsFrm", id = "eProductsFrm" }))
{
// Your code
}
您没有将模型属性与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 />
我已使用此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>
@*
产品名称*@
现在name
和id
是[0].ProductName
更多信息。以便MVC将表单数据绑定到操作方法的参数 他们的名字应该匹配 就你而言:
...
name="model[0].ProductName"
应该是:
...
name="viewModel[0].ProductName"
因为这是您提供的参数的名称:
public ActionResult Save(List<ProductViewModel> viewModel)
public ActionResult保存(列表视图模型)
还要小心,因为索引不应该被破坏,这意味着它们应该是0,1,2。。等等,不跳过一个数字
我们读取属性的方式是通过查找
参数名称[索引]。属性名称。索引必须以零为基础,并且
完整的
你可以在Scott Hanselman的帖子中阅读更多内容-我刚刚写了这篇文章。。。heheSorry,我更改了控制器的名称,但我忘了在表单上更改它。我已经更新了我的问题。对不起,您能展示一下您的ProductViewModel类吗@VansFannelSo页面加载时你的列表为空?而且我没有看到任何在其中放置任何行的内容?当用户提交数据时,列表为空。我不向此视图传递任何数据。这是我第一次使用Razor,也许这就是问题所在。啊,如果你用谷歌搜索asp mvc和表,你可能会发现一些好东西。看看这里的标记答案。不需要,因为所有内容都在@{…}内。这将为输入提供一个id,该id等于模型[0]的值。ProductName,无论该产品的名称是什么@如果属性名位于双引号内,{…}将不适用。此外,带有小m的模型只能用于声明模型。请记住,如果您决定将另一个参数添加到Save Action方法中,这可能不起作用。因此,按名称绑定参数是更好的做法。