I an new to jquery and trying to integrate My MVC Controller with Jquery-Ajax. But is not working properly. Please check with below code.
_ViewStart.cshtml
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Shared/_Layout.cshtml(body section)
<body>
@RenderBody()
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
<script src="~/Scripts/AjaxProgramming.js"></script>
</body>
Controllers/AjaxProgrammingController.cs
public class AjaxProgrammingController : Controller
{
MVCClassesProjectEntities2 mcpe = new MVCClassesProjectEntities2();
public ActionResult Home()
{
Response.Write("Index action method processed at: " + DateTime.Now.ToString());
return View();
}
public ActionResult GetEmpData(int id)
{
Employee empDetails = mcpe.Employees.Find(id);
return Json(empDetails, JsonRequestBehavior.AllowGet);
}
}
Scripts/AjaxProgramming.js
function fn_getEmp()
{
var Id = $("#txtEmpId").val();
$.ajax
({
cache: false,
url: "/AjaxProgramming/GetEmpData/" + Id,
type: "GET",
data: "",
contentType: "application/json;",
success: function (response)
{
var empList = "EmpName : " + response.EmpName + "<br>";
empList += "Job : " + response.Job + "<br>";
empList += "Salary : " + response.Salary + "<br>";
empList += "DeptNo : " + response.DeptNo + "<br>";
$("#spnStatus").html(empList);
}
});
}
Views/Home.cshtml
Enter Employee number : <input type="text" id="txtEmpId" name="txtEmpId" /><br /><br />
<input type="button" id="btnDetails" name="btnDetails" value="Get Emp Details" onclick="fn_getEmp()"/><br /><br />
<span id="spnStatus"></span>
when i click the button i am not getting the result. Please help me.
GetEmpData()method being hit. You need to provide some more detailsRouteConfiginAppStart