简体   繁体   English

从ASP.NET Core MVC中的另一个视图访问一个controller的变量

[英]Access variable of one controller from another view in ASP.NET Core MVC

I am trying build a project while self learning but have been stuck in one place.我正在尝试在自学的同时构建一个项目,但一直停留在一个地方。

I have this class Roles :我有这个 class Roles

namespace IC2021.Models
{
    public partial class Roles
    {
        public Roles()
        {
            Staffs = new HashSet<Staffs>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Staffs> Staffs { get; set; }
    }
}

And another called Staffs :另一个叫Staffs

namespace IC2021.Models
{
    public partial class Staffs
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Location { get; set; }

        public int? RoleId { get; set; }
        public string Archived { get; set; }

        public virtual Roles Role { get; set; }
    }
}

This is my RolesController :这是我的RolesController

namespace IC2021.Controllers
{
    public class RolesController : Controller
    {
        private readonly ICOctober2021Context _context;

        public RolesController(ICOctober2021Context context)
        {
            _context = context;
        }

        // GET: Roles
        public async Task<IActionResult> Index()
        {
            return View(await _context.Roles.ToListAsync());
        }

        public async Task<IActionResult> RolesWithStaffs()
        {
            var iCOctober2021Context = _context.Roles.Include(s => s.Staffs);
            return View(await iCOctober2021Context.ToListAsync());
        }
    }
}

And finally I'm trying to view it from RolesWithStaffs :最后我试图从RolesWithStaffs查看它:

<!-- model declaration -->
@model IEnumerable<IC2021.Models.Roles>

@{
    ViewData["Title"] = "RolesWithViewController";
}

<h1>RolesWithViewController</h1>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                Role
            </th>
            <th>
                Staff Name
            </th>
            <th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Id)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Staffs)
                </td>
               
                @*<td>
                    <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
                    <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
                    <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
                </td>*@
            </tr>
        }
    </tbody>
</table>

So here in the view when I tried to access from Staffs , I am not able (for example item.Staffs.FirstName , or anything else from Staffs ).因此,在视图中,当我尝试从Staffs访问时,我无法访问(例如item.Staffs.FirstName或来自Staffs的任何其他内容)。 Whereas I can do it other way, I mean from staffs view I can access Roles.Name or Id ).虽然我可以用其他方式做到这一点,但我的意思是从员工的角度来看我可以访问Roles.NameId )。

Can anyone please help me?谁能帮帮我吗? Any help will be highly appreciated.任何帮助将不胜感激。

Your view model looks quite unusual, IMHO you can try this您的视图 model 看起来很不寻常,恕我直言,您可以试试这个

public async Task<IActionResult> RolesWithStaffs()
        {
             var model=  await _context.Set<Staffs>().Include(s => s.Role).ToListAsync();
            return View(model);
        }

and view并查看

 
<table class="table">
    <thead>
        <tr>
            <th>
              First Name
            </th>
          <th>
              Last Name
            </th>
            <th>
                 Role 
            </th>
            <th>
            <th></th>
        </tr>
    </thead>
    <tbody>
      @foreach (var item in Model)
      { 
            <tr>
                <td>
                    @Html.DisplayFor(item=> item.FirstName)
                </td>
                <td>
                    @Html.DisplayFor(item=> item.LastName)
                </td>
                <td>
                    @Html.DisplayFor(item => item.Role.Name)
                </td>
               
                <td>
                    <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
                    <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
                    <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
                </td>
            </tr>
        }
    </tbody>
</table>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 ASP.NET Core MVC 中的一个控制器调用或返回另一个控制器的视图 - How to call or return the view of another controller from one controller in ASP.NET Core MVC 如何在ASP.NET MVC 4 Web应用程序中从一个控制器的视图重定向到另一个控制器的视图 - How to redirect to a View of another Controller from one Controller's View in ASP.NET MVC 4 Web Application 将变量从视图传递到 controller 操作方法,然后传递到同一 ASP.NET MVC controller 中的另一个方法 - Pass a variable from a view to a controller action method and then to another method in the same ASP.NET MVC controller ASP.NET Core 6 MVC:如何从 controller 访问 Program.cs 中定义的变量值? - ASP.NET Core 6 MVC : how to access a variable's value that is defined in Program.cs from a controller? asp.net MVC4从另一个视图中的一个视图访问变量(ActionLink) - asp.net MVC4 Access to a variable from a view in another view (ActionLink) ASP.NET MVC:视图中的访问控制器实例 - ASP.NET MVC: Access controller instance from view ASP.NET - MVC 4使用从控制器到视图的变量 - ASP.NET - MVC 4 using a variable from a controller to a view ASP.NET MVC 将值从控制器上的变量传递到视图 - ASP.NET MVC passing a value from a variable on a controller to a view 如何将数据从一个视图显示到另一个 ASP.NET CORE 5.0 MVC - How to display data from one View to another ASP.NET CORE 5.0 MVC 在ASP.NET MVC中将数据从一个控制器传递到另一个 - Passing data from one controller to another in asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM