Gautam Puhan

Gautam Puhan

  • NA
  • 43
  • 1.7k

I am new to mvc. i can't find the solution.???

Mar 23 2018 12:33 AM
controller code
 
public class EmployeeController : Controller
{
LoginDB2Entities1 ld = new LoginDB2Entities1();
[HttpGet]
public ActionResult GetByEmployeeID()
{
return View();
}
[HttpPost]
public ActionResult GetByEmployeeID(int id)
{
using (LoginDB2Entities1 ldb = new LoginDB2Entities1())
{
var deptemp = from e in ldb.Depts
join o in ldb.Emps on e.Did equals o.Did
where e.Did == id
select new
{
o.Salary,
e.Did,
o.EmpId,
o.EmpName,
o.Empjob,
e.DName,
};
var empdetails = deptemp.FirstOrDefault();
return Json(new { empdetails });
}
}
public ActionResult DisplayAll()
{
var emp = ld.Emps.Include(a => a.Dept);
return View(emp.ToList());
}
}
}
 
view code
 
@{
ViewBag.Title = "GetByEmployeeID";
}
<h2>GetByEmployeeID</h2>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<div>
<div id="did"></div>
<div id="dname"></div>
<div id="empId"></div>
<div id="eName"></div>
<div id="eJob"></div>
<div id ="salry"> </div>
<label>Enter Id: </label>
<input type="text" id="txtId" placeholder="Enter Id"/>
<input type="button" value="Get Details" id="btnSubmit"/>
</div>
<script>
$(document).ready(function () {
$("#btnSubmit").click(function () {
var id = $("#txtId").val();
$.ajax({
type: "POST",
url: "/Employee/GetByEmployeeID",
contentType: "application/json;character=utf-8",
dataType: "json",
data: JSON.stringify({ "id": id }),
success: function (item) {
if (item != null) {
$("#did").val(item.empdetails.Did);
$("#dname").val(item.empdetails.Dname);
$("#empId").val(item.empdetails.EmpId);
$("#eName").val(item.empdetails.EmpName);
$("#eJob").val(item.empdetails.EmpJob);
$("#salry").val(item.empdetails.Salary);
}
else {
alert("Some error occur")
}
},
error:function(item){}
})
});
});
</script>

Answers (4)