在MVC控制器中,我试图将一组数据传回视图。
private string GetEntityList()
{
var entities = new[]
{
new EntityList{ EntityId = 1, EntityName = "Entity 1"},
new EntityList{ EntityId = 2, EntityName = "Entity 2"},
new EntityList{ EntityId = 3, EntityName = "Entity 3"},
new EntityList{ EntityId = 4, EntityName = "Entity 4"},
new EntityList{ EntityId = 5, EntityName = "Entity 5"}
};
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
return JsonConvert.SerializeObject(entities, Formatting.None, settings);
}
我通过字符串模型将其发送到视图
public ViewResult Index()
{
return View("index","", GetEntityList());
}
在我的cshtml文件中,我试图将模型分配给名为mvcData的属性,即我定义的服务的一部分
app.factory("searchAttributes", function() {
console.log(@Html.Raw(Model));
return { mvcData:@Html.Raw(Model) };
});
</script>
此脚本标记位于我的ng-app内部,因此它位于应用范围内,但是当我尝试引用app.controller
时它是否显示为未定义
app.controller('searchController', ['$scope', 'searchService', 'ngNotifier', '$log', '$timeout', 'searchAttributes', function ($scope, searchService, searchAttributes, ngNotifier, $log, $timeout) {
var vm = this;
//bootstraped data from MVC
$scope.searchAttributes = searchAttributes.mvcData;
}]);
当模型返回到html时,我可以看到它是一个有效的对象。使用console.log(@Html.Raw(Model)
我看到对象回来了
Object[0] entityId: 1 entityName: "Entity 1"
Object[1] entityId: 2 entityName: "Entity 2"
为什么没有在控制器中拾取此对象的任何想法?即使我将它定义为依赖项,就好像ng-controller
没有检测到它/加载它一样。
先谢谢
答案 0 :(得分:1)
您的依赖项无序:
app.controller('searchController', ['$scope', 'searchService', 'ngNotifier', '$log', '$timeout', 'searchAttributes', function ($scope, searchService, searchAttributes, ngNotifier, $log, $timeout) {
应该是
app.controller('searchController', ['$scope', 'searchService', 'ngNotifier', '$log', '$timeout', 'searchAttributes', function ($scope, searchService, ngNotifier, $log, $timeout, searchAttributes) {
您当前的代码意味着您的searchAttributes.mvcData;
实际上是引用timeout.mvcData
未定义的。{/ p>