【问题标题】:Access variable of one controller from another view in ASP.NET Core MVC从 ASP.NET Core MVC 中的另一个视图访问一个控制器的变量
【发布时间】:2021-11-28 09:18:26
【问题描述】:

我正在尝试在自学的同时构建一个项目,但一直卡在一个地方。

我有这门课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)。

谁能帮帮我?任何帮助将不胜感激。

【问题讨论】:

  • 有什么问题?错误?预期的输入/输出?

标签: c# asp.net-mvc model-view-controller view controller


【解决方案1】:

你的视图模型看起来很不寻常,恕我直言,你可以试试这个

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 2017-03-23
    相关资源
    最近更新 更多