I am trying to append a table row which has a form and input type submit. Typically, this is what my loop looks like:
@foreach (var item in Model)
{
using (Html.BeginForm("Edit", "User"))
{
<tr>
<input type="hidden" name="Id" value="@item.Id" />
<td>
@Html.DisplayFor(modelItem => item.Email)
@Html.HiddenFor(modelItem => item.Email, new { name = "Email" })
</td>
<a href="@Url.Action("Edit", new { Id = item.Id })">Edit</a>
<button type="submit" onclick="return confirm('Disable this?');" name="btnDisable" id="btnDisable">Disable</button>
</td>
</tr>
}
}
Now on my AJAX call, I need to add additional user here on this table row, here's how I usually do it:
$.ajax({
type: "POST",
url: 'URL',
dataType: 'json'
}).done(function (data) {
if (data.status == 'True') {
var usrObj = data.uobj;
$(function () {
var table = $('#tblUsers tbody');
for (var i = 0; i < usrObj.length; i++) {
$('#tblUsers').find('tbody')
.append('<tr><form action="/User/Edit" method="post">' +
'<input type="hidden" name="Id" value=' + usrObj[i].Id + '>' +
'<td>' + usrObj[i].Email + '</td>' +
"<td><a href='" + "/User/Edit/" + usrObj[i].Id + "'" + "></i>Edit</a>" +
`<button type='submit' name='btnDisable' id = 'btnDisable' onclick="return confirm('Disable this?')";>Disable</button></td>` +
'</form></tr>');
}
}
});
Now what is happening is that, the appended link is working fine since everything the link that is needed to function is already set on it's attributes:
<a href='" + "/UserM/Edit/" + usrObj[i].Id + "'" + "></i>Edit</a>
This is I am having a hard time submitting the form:
<button type='submit' name='btnDisable' id = 'btnDisable' onclick="return confirm('Disable this?')";>Disable</button>
I tried adding the following code below but still, nothing is happening, the confirm dialog box is popping up, but upon clicking Ok button, nothing is happening. The form is not submitting. But the other rows are working fine after the append.
Here's what I tried, but all of these are not working fine.
$(function () {
$(document).on('submit', 'form', function (event) {
$(this).submit();
});
});
I guess the way I appended my table row is the problem but I am not clearly sure.