简体   繁体   English

如何在 Vue 实例中使用 @ViewBag?

[英]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.

相关问题 如何在控制器方法中使用文本框和ViewBag值? - How to use textbox and ViewBag value in controller method? 如何使用ViewBag存储字符串数组 - How to use the ViewBag to store an array of strings 如何分配列表 <string> 到ViewBag并在JavaScript中使用? - How to assign a List<string> to a ViewBag and use in JavaScript? 在Viewbag中使用扩展方法 - Use an extension method in a Viewbag 如何在Jquery Selector中使用c#ViewBag变量? - How can I use c# ViewBag variable in Jquery Selector? ViewBag,ViewData,TempData,Session - 如何以及何时使用它们? - ViewBag, ViewData, TempData, Session - how and when to use them? 应该传递什么对象进行序列化(),以及如何在Knockoutjs文件中使用ViewBag? - What object should be passed to serialize () and how to use ViewBag in Knockoutjs file? 如何使用Razor EditorFor Helper创建带有ViewBag中的值的文本框 - How to use Razor EditorFor Helper to create a TextBox with a value in from ViewBag 使用方法(IEnumerable <SelectListItem> ViewBag在数据库中显示保存的项目 - how to use (IEnumerable<SelectListItem>)ViewBag to show saved item in data base 如何将 ViewBag 中的详细信息转换为用于 FileStreamResult 的字符串? - How do I get details from ViewBag into a string for use in FileStreamResult?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM