While performing crude operations in the gridview you can also use jquery code for the editing updating and the deletion of the details.
<script>
$(document).ready(function () {
$(".editLink").click(function (e) {
var a = $(this).attr('name');
$("#lblEmpId" + a).hide();
$("#txtEmpId" + a).show();
$("#lblFirstName" + a).hide();
$("#txtFirstName" + a).show();
$("#lblMiddleName" + a).hide();
$("#txtMiddleName" + a).show();
$("#lblLastName" + a).hide();
$("#txtLastName" + a).show();
$("#lblAddress" + a).hide();
$("#txtAddress" + a).show();
$("#lblPhoneNo" + a).hide();
$("#txtPhoneNo" + a).show();
e.preventDefault();
$("#editlnk" + a).hide();
$("#updlnk" + a).show();
$("#cancellnk" + a).show();
});
$(".updateLink").click(function (e) {
var a = $(this).attr('name');
alert(a);
var EmployeeModel = {};
EmployeeModel.EmpId = $("#txtEmpId" + a).val();
EmployeeModel.FirstName = $("#txtFirstName" + a).val();
EmployeeModel.MiddleName = $("#txtMiddleName" + a).val();
EmployeeModel.LastName = $("#txtLastName" + a).val();
EmployeeModel.Address = $("#txtAddress" + a).val();
EmployeeModel.PhoneNo = $("#txtPhoneNo" + a).val();
$.ajax({
type: "POST",
url: "/RegisterGrid/UpdateUser",
data: '{EmployeeDetails: ' + JSON.stringify(EmployeeModel) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d);
window.location.reload();
},
error: function (response) {
alert("Some error occurred", "Error Description" + response.d);
window.location.reload();
}
});
});
</script>
These script needs to be placed in the view for the select operation when griview is bind .
@foreach (var item in Model)
{
<tr>
<td>
@Html.Label("", Convert.ToString(item.EmpId), new { id = "lblEmpId" + i })
</td>
<td>
@Html.Label("", item.FirstName, new { id = "lblFirstName" + i })
</td>
<td>
@Html.Label("", item.MiddleName, new { id = "lblMiddleName" + i })
</td>
<td>
@Html.Label("", item.LastName, new { id = "lblLastName" + i })
</td>
<td>
@Html.Label("", item.PhoneNo, new { id = "lblPhoneNo" + i })
</td>
<td>
@Html.Label("", item.Address, new { id = "lblAddress" + i })
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id =item.EmpId, @class = "editLink" }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { id=item.EmpId })
</td>
</tr>
i = i + 1;
}
</table>
}
After this we can get the ajax call for doing any operation we want.
0 Comment(s)