[英]How to use @ViewBag into a Vue instance?
I'm a new trainee and I'm studing ASP.NET Core, EF Core and Vue.我是一名新实习生,正在学习 ASP.NET Core、EF Core 和 Vue。 After to have played a bit with EF Core and Repository Pattern, I'm trying to integrate this with Vue.js for the Front-End, but I have some issues with it.
在玩了一些 EF Core 和存储库模式之后,我试图将它与 Vue.js 集成到前端,但我有一些问题。
Susbstantially, I can't to use @ViewBag, where I put a list of something (Users by Model), into instance of Vue.基本上,我不能使用@ViewBag,我在其中将一些东西(按模型的用户)的列表放入 Vue 的实例中。
Controller: Controller:
public IActionResult Index()
{
IEnumerable<Users> allUsers = _userRepository.GetUsersOrderedByUsername(); //List of Users
ViewBag.allUsers = allUsers;
return View();
}
While in the View I have this在视图中我有这个
View:看法:
var usersVM = new Vue({
el: '#users',
data: {
allUsers: @ViewBag.allUsers
}
})
The problem is that when I try to use it like a list of Users's instances, it rightly doesn't see like a list问题是当我尝试像用户实例列表一样使用它时,它看起来不像列表
I would want show a list of users in this way我想以这种方式显示用户列表
View:看法:
<div id="users" class="text-left">
<ul>
<li v-for="u in allUsers">
{{ u.Username }} //Users Model has one Username attribute
</li>
</ul>
</div>
How can I resolve this?我该如何解决这个问题?
Thank you.谢谢你。
I resolved, if it can be helpful I had to code this in the Vue instance that I created:我解决了,如果有帮助,我必须在我创建的 Vue 实例中对此进行编码:
allUsers: JSON.parse('@Html.Raw(Json.Serialize(ViewBag.allUSers))')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.