While writing codes in MVC there is need for using javascript in the code.
So for doing that we need to put the library for it and adding the library bundle into the code.
$(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();
}
});
$("#lblEmpId" + a).show();
$("#txtEmpId" + a).hide();
$("#lblFirstName" + a).show();
$("#txtFirstName" + a).hide();
$("#lblMiddleName" + a).show();
$("#txtMiddleName" + a).hide();
$("#lblLastName" + a).show();
$("#txtLastName" + a).hide();
$("#lblAddress" + a).show();
$("#txtAddress" + a).hide();
$("#lblPhoneNo" + a).show();
$("#txtPhoneNo" + a).hide();
$("#editlnk" + a).show();
$("#updlnk" + a).hide();
$("#cancellnk" + a).hide();
e.preventDefault();
});
$(".cancelLink").click(function (e) {
var a = $(this).attr('name');
$("#lblEmpId" + a).show();
$("#txtEmpId" + a).hide();
$("#lblFirstName" + a).show();
$("#txtFirstName" + a).hide();
$("#lblMiddleName" + a).show();
$("#txtMiddleName" + a).hide();
$("#lblLastName" + a).show();
$("#txtLastName" + a).hide();
$("#lblAddress" + a).show();
$("#txtAddress" + a).hide();
$("#lblPhoneNo" + a).show();
$("#txtPhoneNo" + a).hide();
$("#editlnk" + a).show();
$("#updlnk" + a).hide();
$("#cancellnk" + a).hide();
e.preventDefault();
});
});
So for this we use the javascript for the enabling and disabling the elements and adding the bundle for the javascript .
@model MVC_Demo.Models.EmployeeModel
@{
ViewBag.Title = "RegisterEmployee";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<h2>RegisterEmployee</h2>
@using (Html.BeginForm("RegisterEmployeeDetails", "Register", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>EmployeeModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => Model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => Model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => Model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => Model.MiddleName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => Model.MiddleName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => Model.MiddleName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => Model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => Model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => Model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => Model.PhoneNo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => Model.PhoneNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => Model.PhoneNo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => Model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => Model.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => Model.Address, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
0 Comment(s)