【发布时间】:2016-05-06 18:14:08
【问题描述】:
我在 mvc 中有动作,当页面请求时触发,然后我生成模型的 json 并存储在 viewbag 中。在剃刀视图中,我将 json 从 viewbag 存储到角度模型。我的代码假设填充下拉但没有得到正确的输出。请告诉我哪里出错了。
MVC 动作
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Countries = GetCountries();
return View();
}
public JsonResult GetCountries()
{
List<Country> oList = new List<Country>()
{
new Country {ID=1,Name="United Kingdom"},
new Country {ID=1,Name="United States"},
new Country {ID=1,Name="Italy"},
new Country {ID=1,Name="Germany"},
new Country {ID=1,Name="India"}
};
return Json(oList);
}
}
用于填充下拉列表的 AngularJS 代码
<div class="row">
<div ng-app="drpdwnApp" ng-controller="drpdwnCtrl">
<select ng-model="ID" ng-options="item.Name for item in CountryList">
<option value="" >--Select--</option>
</select>
</div>
</div>
@section scripts
{
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script type="text/javascript">
angular.module('drpdwnApp', []).controller('drpdwnCtrl', function ($scope, $http) {
$scope.CountryList = null;
$scope.fillCountryList = function () {
$scope.CountryList = JSON.parse('@Html.Raw(Json.Encode(ViewBag.Countries))');
};
$scope.fillCountryList();
});
</script>
}
【问题讨论】:
标签: angularjs json asp.net-mvc