I have a small table as part of a form. It displays a number alongside a dropdown that allows a user to select a value for to go along with that number. I want to allow the user use add rows to this table and be able to assign as many values as is required. I've been searching Google all day and trying many things but nothing seems to fit my situation. What is the best way to achieve this functionality?
Here is the code from my view containing the table and my add actionlink:
<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>
And here is my controller method from for the action link:
public ActionResult AddNewPartConnection(IndexViewModel model)
{
var newPartConnectionType = new PartConnectionType();
model.Part.PartConnectionTypes.Add(newPartConnectionType);
return View("Index", model);
}
and the index method:
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);
}
I appreciate any help anyone can provide.