I'm facing an issue where the viewbag value is not being passed as a parameter in ng-init. Can someone guide me on how I can successfully pass the viewbag value as a parameter?
angular.js
{ $scope.detail = function (Id)
{
debugger
$http.post('/Employees/GetDetail', JSON.stringify({ id: Id }))
.success(function (result) {
debugger
$scope.ampdetail = result;
$window.location.href = 'Detail/' + Id;
})
.error(function (result) {
console.log(result);
});
}
This is my Controller:
My Controller
public ActionResult Detail(int empid)
{
ViewBag.empid = empid;
return View();
}
[HttpPost]
public JsonResult GetDetail(int id)
{
var employee = (from x in db.employees where x.Id==id
select new {
Id = x.Id,
DeptName = x.TblDepartment.Name,
Name = x.Name,
Salary = x.Salary,
Gender = x.Gender,
City = x.City,
Age = x.Age,
image = x.image
}
);
return Json(employee, JsonRequestBehavior.AllowGet);
}
----------
view
--------
<!DOCTYPE html >
<html>
<head>
<title>Edit Employee</title>
</head>
<body ng-app="app" ng-controller="myController" data-ng-init="detail(@ViewBag.empid)"> // Encountering issues with passing id here??
<h2>Details</h2>
<div>
<h4>Employee</h4>
<hr />
<dl class="dl-horizontal">
<dt>
<label style="display:none">Id</label>
</dt>
<dd style="display:none">
{{ampdetail[0].Id}}
</dd>
<dt>
<label class="control-label col-md-2">DeptName</label>
</dt>
<dd>
{{ampdetail[0].DeptName}}
</dd>
<dt>
<label class="control-label col-md-2">Name</label>
</dt>
<dd>
{{ampdetail[0].Name}}
</dd>
<dt>
<label class="control-label col-md-2">Gender</label>
</dt>
<dd>
{{ampdetail[0].Gender}}
</dd>
<dt>
<label class="control-label col-md-2">Age</label>
</dt>
<dd>
{{ampdetail[0].Age}}
</dd>
<dt>
<label class="control-label col-md-2">Salary</label>
</dt>
<dd>
{{ampdetail[0].Salary}}
</dd>
<dt>
<label class="control-label col-md-2">City</label>
</dt>
<dd>
{{ampdetail[0].City}}
</dd>
<dt>
<label class="control-label col-md-2">Image</label>
</dt>
<dd>
{{ampdetail[0].image}}
</dd>
</dl>
</div>
@*<script src="~/Scripts/jquery-1.10.2.js"></script>
<script>
document.ready(function () {
alert('@ViewBag.empid')
});
</script>*@
</body>
</html>
}