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

时间:2016-10-07 17:22:45

标签: c# asp.net-mvc linq

我正在试图弄清楚为什么所有内容都会编译,页面加载,但 Managers 下拉菜单中没有任何内容。 工程师 dropdown正确填充,但不包括经理。我认为也许查询没有返回结果,但是应该返回 2 ,我在表格中显示了计数并验证了 2 匹配。为什么这不起作用?

   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();
    }

这是用于创建的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>

2 个答案:

答案 0 :(得分:0)

您是不是要在 ViewBag.ManagerId 的第一行实例化一个新的经理列表?

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");
    }

答案 1 :(得分:0)

原来我在显示的变更模型中缺少正确的ForeignKey注释。

E     F         G
111   20050719  Foreign
112   20050719  Domestic
576   20050719  Foreign
624   19910101  Domestic

$E$89 = 20050719