简体   繁体   English

尽管SelectList中计数为2,但下拉列表中未列出的名称

[英]Names Not Listed in Dropdown Despite a Count of 2 in SelectList

I'm trying to figure out why everything compiles, the page loads, but there's nothing in the Managers drop down menu. 我试图弄清楚为什么所有内容都会编译,页面加载,但是“ 管理器”下拉菜单中没有任何内容。 The Engineers dropdown populates properly, but not the Managers . 工程师 dropdown正确填充,但经理填充不会。 I thought that perhaps the query returned no results, but as it should, it returned 2 , I displayed the count and verified 2 matches in the table. 我认为查询可能未返回任何结果,但应返回2 ,因此我在表中显示了计数并验证了2个匹配项。 Why would this not work? 为什么这不起作用?

   public ActionResult Create()
    {
        //var userId = User.Identity.GetUserId();
        //var user = 
        ViewBag.StatusId = new SelectList(db.statuses, "StatusId", "StatusName");
        ViewBag.SystemDetailId = new SelectList(db.systems, "SystemDetailId", "SystemName");


        var engineers = db.engineers;
        var managers = db.managers;
        var mte = db.ManagersToEngineers;
        List<Manager> matchedManager = null;
        Engineer matchedEngineer = null;

        if (this.User.Identity.IsAuthenticated)
        {
            var userEmail = this.User.Identity.Name;
            matchedEngineer = engineers.Where(x => x.email == userEmail).FirstOrDefault();
            if (matchedEngineer != null)
            {
                matchedManager = mte.Where(x => x.EngineerId == matchedEngineer.PersonId).Select(x => x.manager).ToList();
            }

        }

        if (matchedEngineer != null)
        {
            ViewBag.EngineerId = new SelectList(new List<Engineer> { matchedEngineer }, "PersonId", "FullName");
            ViewBag.ManagerId = new SelectList(matchedManager, "PersonId", "FullName");
        }
        else
        {
            ViewBag.EngineerId = new SelectList(engineers, "PersonId", "FullName");
            ViewBag.ManagerId = new SelectList(managers, "PersonId", "FullName");
        } 

        return View();
    }

Here's the cshtml for create (UPDATE: Engineer drop down shared for reference): 这是用于创建的cshtml(更新:工程师下拉菜单供参考):

    <div class="form-group">
        @Html.LabelFor(model => model.EngineerId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("EngineerId", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.engineer.FullName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ManagerId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("ManagerId", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.manager.FullName, "", new { @class = "text-danger" })
        </div>
    </div>

Did you mean to instantiate a new List of Manager in the first line of ViewBag.ManagerId ? 您是要在ViewBag.ManagerId的第一行中实例化新的Manager列表吗?

if (matchedEngineer != null)
    {
        ViewBag.EngineerId = new SelectList(new List<Engineer> { matchedEngineer }, "PersonId", "FullName");
        // new up list below
        ViewBag.ManagerId = new SelectList(new List<Manager>, "PersonId", "FullName");
    }

Turns out I was missing the proper ForeignKey annotations in the change model it was being displayed from. 原来,我在从中显示更改模型时缺少正确的ForeignKey批注。

public class ManagerToEngineer
{
    [Key]
    public int ManagerToEngineerId { get; set; }

    [ForeignKey("engineer")]
    public int EngineerId { get; set; }

    [ForeignKey("manager")]
    public int ManagerId { get; set; }

    public virtual Engineer engineer { get; set; }
    public virtual Manager manager { get; set; }

}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM