如何将angularjs函数中的viewbag值作为参数传递?

时间:2016-05-24 07:48:25

标签: javascript angularjs viewbag

我遇到问题,因为viewbag没有将id作为参数传递给ng-init。 请告诉我如何将viewbag作为参数传递?

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);
               });
           }

这是我的控制器:

我的控制器

       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)">  //this where i am having problem it dosen't pass id??
    <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>

    }

1 个答案:

答案 0 :(得分:0)

据我所知,由于视图不是剃刀,所以你不能。