【发布时间】:2014-10-30 20:04:26
【问题描述】:
我有一张小桌子作为表格的一部分。它在下拉列表旁边显示一个数字,允许用户选择一个与该数字一起使用的值。我希望允许用户使用向该表添加行并能够根据需要分配尽可能多的值。我整天都在搜索谷歌并尝试了很多东西,但似乎没有什么适合我的情况。实现此功能的最佳方法是什么?
这是我的视图中包含表格和添加操作链接的代码:
<div class="form-group">
@Html.LabelFor(m=>m.Part.PartConnectionTypes, new {@class = "control-label col-md-5"})
<div class="col-md-7">
<table class="table table-condensed table-striped table-bordered">
<thead>
<tr>
<td>Number</td>
<td>Value</td>
</tr>
</thead>
<tbody>
@{
for(var i = 0; i < Model.Part.PartConnectionTypes.Count; i++)
{
var count = i + 1;
<tr>
<td>@count</td>
<td>@Html.DropDownListFor(m => m.Part.PartConnectionTypes[i].ConnectionType, new SelectList(Model.ConnectsTo, "CodeId", "ValChar"), "", new { @class = "form-control" })</td>
</tr>
}
}
</tbody>
</table>
@Html.ActionLink("Add Connection", "AddNewPartConnection", "Home", new { id = "addConnection"})
</div>
这是我用于操作链接的控制器方法:
public ActionResult AddNewPartConnection(IndexViewModel model)
{
var newPartConnectionType = new PartConnectionType();
model.Part.PartConnectionTypes.Add(newPartConnectionType);
return View("Index", model);
}
和索引方法:
public ActionResult Index(string searchString)
{
var model = new IndexViewModel { Part = new Part() };
model.Part.PartConnectionTypes.Add(new PartConnectionType());
if (!String.IsNullOrEmpty(searchString))
{
model.Part = CoreLogic.GetPartByPartCode(searchString);
if (model.Part == null)
{
model.Part = new Part();
model.Part.PartConnectionTypes.Add(new PartConnectionType());
ViewBag.SearchMessage = "That is not a current part";
}
}
return View(model);
}
感谢任何人提供的任何帮助。
【问题讨论】:
-
为什么不直接使用 webgrid 来做到这一点:c-sharpcorner.com/UploadFile/2b481f/…?该链接向您展示了这个概念,其余的都在谷歌上。 Bhavin 的答案也应该有效,但是除非 webgrid 不符合您的需求,否则为什么要重新发明轮子。
-
如果要动态添加新行并回发整个集合,则需要使用 javascript/jquery。 This answer 可能会帮助您入门。
标签: javascript c# jquery asp.net-mvc html.dropdownlistfor