troubleshooting Question

ASP .NET Core 2.1 - How to dynamically remove html table rows while maintaining proper indexing

Avatar of nightshadz
nightshadzFlag for United States of America asked on
JavaScript.NET ProgrammingC#ASP.NET
2 Comments1 Solution297 ViewsLast Modified:
In the below example code using ASP .NET Core 2.1, I am dynamically adding a table row when the add button is clicked. The row contains 3 input boxes and one button (a minus symbol which removes the row when clicked). I'm struggling to find the best way to handle the deletion since I want to preserve model-binding when the form is posted. The rows in this table should populate List<People> when the form is submitted.

Is there a more elegant way to handle this besides re-indexing each row every time a delete happens? Any examples I can review will be much appreciated.


Initial view from HttpGet controller
<div>
	<table class="table table-condensed" id="people-list">
		<thead>
		<tr>
			<th>First Name</th>
			<th>Last Name</th>
			<th>Email Address</th>
		</tr>
		</thead>
		<tbody>
		@for (var i = 0; i < Model.PeopleList.Count; i++)
		{
			<tr>
				<td>
					@Html.HiddenFor(m => Model.PeopleList[i].FirstName)
					@Html.TextBoxFor(m => Model.PeopleList[i].FirstName, new { @class = "row-index form-control"})
				</td>
				<td>
					@Html.HiddenFor(m => Model.PeopleList[i].LastName)
					@Html.TextBoxFor(m => Model.PeopleList[i].LastName, new { @class = "form-control" })
				</td>
				<td>
					@Html.HiddenFor(m => Model.PeopleList[i].Email)
					@Html.TextBoxFor(m => Model.PeopleList[i].Email, new { @class = "form-control" })
				</td>
				<td>
					<button class="btn btn-default" id="remove-person-@i"><i class="glyphicon glyphicon-minus"></i></button>
				</td>
			</tr>
		}                    
		</tbody>
	</table>
</div>
<div>
	<div class="btn-group">
		<button type="button" class="btn btn-primary" id="add-person"><i class="glyphicon glyphicon-plus"></i>Add Person</button>
		<br/><br/>
	</div>
</div>

Add new row
$("#add-person").click(function() {
    var i = $(".row-index").length;
    $.ajax({
        url: "people/AddPerson?index=" + i,
        success: function (data) {
            $('#people-list > tbody').append(data);
        },
        error: function (a, b, c) {
            console.log(a, b, c);
        }
    });  
});

Partial View that returns a template row
<tr>
    <td>
        <input id="People_@(ViewBag.Index)__FirstName" name="People[@(ViewBag.Index)].FirstName" type="hidden" value="Bob">
        <input id="People_@(ViewBag.Index)__FirstName" name="People[@(ViewBag.Index)].FirstName" type="text" value="Bob" class="form-control">
    </td>
    <td>
        <input id="People_@(ViewBag.Index)__LastName" name="People[@(ViewBag.Index)].LastName" type="hidden" value="Smith">
        <input id="People_@(ViewBag.Index)__LastName" name="People[@(ViewBag.Index)].LastName" type="text" value="Smith" class="form-control">
    </td>
    <td>
        <input id="People_@(ViewBag.Index)__Email" name="People[@(ViewBag.Index)].Email" type="hidden" value="bsmith@yopmail.com">
        <input id="People_@(ViewBag.Index)__Email" name="People[@(ViewBag.Index)].Email" type="text" value="bsmith@yopmail.com" class="form-control">
    </td>
    <td>
        <button class="btn btn-default" id="remove-person"><i class="glyphicon glyphicon-minus"></i></button>
    </td>
</tr>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros