0

嗨,我有一个让我发疯的问题。我的项目的一个页面中有一个下拉列表,它应该像所有其他页面上的所有其他下拉列表一样,但它没有,我想知道为什么。

在这里,您将看到 cshtml 页面中的代码。这是一个正常的下拉列表:

<p>
    Specify a organisation to search in
    @Html.DropDownList("OrganisationList", new SelectList(ViewBag.OrganisationList, "ID", "Name", ViewBag.SelectedOrganisation))
</p>

在控制器中,我有一个绑定下拉列表的方法和一个调用视图的方法:

private void BindAllOrganisations()
{
    List<OrganisationModel> OrganisationList = new List<OrganisationModel> { new OrganisationModel { ID = null, Name = "Select" } };

    foreach (var Organisation in DatabaseConnection.DataContextRead.GetTable<Organisation>())
    {
        OrganisationList.Add(new OrganisationModel { ID = Organisation.OrganisationId, Name = Organisation.Name });
    }

    ViewBag.OrganisationList = OrganisationList;
}

/**
 * Loads the ManageUserView with all users from a specific organisation unit
 **/
[AzaraAuthorization(RequiredRoles = new[] { RolesEnum.Admin })]
public ActionResult ManageUsersFromOrgUnit(int Id)
{
    try
    {
        var orgUnit = organisationUnitManagement.GetSingleUnit(Id);
        List<AzaraUser> AzaraUserList = azaraUserManagement.GetAllUserFromOrganisationUnit(orgUnit);
        ViewBag.FromOrgUnits = Id;
        BindAllOrganisations();
        ViewBag.SelectedOrganisation = orgUnit.Organisation.OrganisationId;
        return View("ManageUsers", AzaraUserList);
    }
    catch (BusinessObjectNotRetrievableByIdException exc)
    {
        ExceptionHandling.BusinessObjectNotRetrievableByIdExceptionOccured(exc, "ManageUsersFromOrgUnit(int Id)", "ManageUsersController");
        return RedirectToAction("Index", "Home", new { Info = "A program error occured. The administrator get informed. Apologize the inconvenience." });
    }
}

我找不到问题。此外,如果我ViewBag.SelectedOrganisation在页面上替换为(我知道后面的数据库中1有一个带有值的模型),它会选择带有. 这是列表中使用的模型:1null

public class OrganisationModel
{
    public int? ID { get; set; }
    public string Name { get; set; }
}

可能有错误或我错过了什么?

4

0 回答 0