簡體   English   中英

從ASP.NET Core MVC中的另一個視圖訪問一個controller的變量

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

我正在嘗試在自學的同時構建一個項目,但一直停留在一個地方。

我有這個 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; }
    }
}

另一個叫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; }
    }
}

這是我的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());
        }
    }
}

最后我試圖從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>

因此,在視圖中,當我嘗試從Staffs訪問時,我無法訪問(例如item.Staffs.FirstName或來自Staffs的任何其他內容)。 雖然我可以用其他方式做到這一點,但我的意思是從員工的角度來看我可以訪問Roles.NameId )。

誰能幫幫我嗎? 任何幫助將不勝感激。

您的視圖 model 看起來很不尋常,恕我直言,您可以試試這個

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

並查看

 
<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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM