【发布时间】:2017-02-15 10:55:47
【问题描述】:
正在尝试使用 Html.BeginCollectionItem 在我的应用程序中工作,并且正在努力获取要发布的数据。我想将项目添加到列表中,然后发布整个列表。我正在使用 ajax 和 jquery 在我的列表中添加和删除项目,这似乎有效。但是当我发布在我的控制器中收到的模型时,即使当我查看提琴手时,表单数据也包含我的所有信息。
有没有人在我的代码中看到我做错了一些简单的事情?
主视图:
@model Test.Models.TestList
@using (Html.BeginForm())
{
<div class="form-group">
<div class="row">
<label for="AssemblyText" class="col-sm-1 col-sm-offset-1 control-label">Assembly:</label>
<div class="col-sm-2">
<input type="text" id="assembly" />
</div>
<label for="QuantityText" class="col-sm-1 control-label">Quantity:</label>
<div class="col-sm-2">
<input type="text" id="Qty" />
</div>
<button type="button" id="AddAssembly">Add Button</button>
</div>
</div>
<table id="Assemblies" class="table table-striped">
<thead>
<tr>
<th>Assembly</th>
<th>Quantity</th>
<th>Action</th>
</tr>
</thead>
<tbody class="text-left">
@if (Model != null)
{
foreach (var assembly in Model.mylist)
{
@Html.Partial("AssemblyRow", assembly)
}
}
</tbody>
</table>
<div class="form-group">
<input type="submit" id="submitbtn" class="btn btn-success" value="Submit" />
</div>
}
局部视图(AssemblyRow)
@model Test.Models.Test
<tr class="editorRow">
@using (Html.BeginCollectionItem("Assembly"))
{
<td>
@Html.HiddenFor(m => m.assembly)
@Html.TextBoxFor(m => m.assembly)
</td>
<td>
@Html.HiddenFor(m => m.Qty)
@Html.TextBoxFor(m => m.Qty)
</td>
<td>
<span class="dltBtn">
<a href="#" class="deleteRow">Delete</a>
</span>
</td>
}
我的模型很简单,看起来像...
public class TestList
{
public List<Test> mylist { get; set; }
}
public class Test
{
public string assembly { get; set; }
public string Qty { get; set; }
}
我的控制器
[HttpPost]
public ActionResult PostMain(TestList model)
{
return View();
}
我可以提供你们认为有帮助的任何其他代码,但我尽量用我认为相关的部分来保持简单。
感谢您的帮助!
编辑:提琴手的图片
【问题讨论】:
标签: c# asp.net-mvc