I am trying to add a remove/delete a dynamically created partial view. This is my ADD script.
$("#btnAdd").on('click', function () {
$.ajax({
async: false,
url: '/Employees/Add'
}).success(function (partialView) {
$('#AddSchedule').append("<tbody>" + partialView + "</tbody>");
});
});
this is the add controller
public ActionResult Add()
{
var schedViewModel = new FacultySchedViewModel();
return PartialView("~/Views/Employees/_DynamicView.cshtml", schedViewModel);
}
this is the partial view _DynamicView.cshtml
@using(Html.BeginCollectionItem("New")){
<td>
@Html.ActionLink("Delete", "DeleteThis", "MyController", null)
</td>
<td>
@Html.EditorFor(model => @Model.Schedule, new { htmlAttributes = new { @class = "form-control" } })
</td> }
what i can't figure out are
- how to get the ID generated by BeginItemCollection
- use the ID in a remove script
- action on the controller
EDIT 1. How to connect it to a button or a link for removing the row
Added the view on the the Main of the partial view
@for (int i = 0; i < @Model.New.Count(); i++)
{
@Html.EditorFor(model => @Model.New[i])
}