下拉列表值未传递到控制器

dig*_*doe 1 asp.net-mvc-4

这个问题应该非常简单..我试图将视图中下拉列表中的值传递给控制器​​..我没有收到错误,但它为该属性发送了空值。请帮忙..

我的代码如下:

控制器:

    public ActionResult Create()
    {
        var list = new [] 

            { 
                new Room{ RoomID = 1, Building = "FAYARD HALL"},
                new Room{ RoomID = 2, Building = "WHATEVER HALL"},
                new Room{ RoomID = 3, Building = "TIME SQUARE"},
                new Room{ RoomID = 4, Building = "MISSISSIPPI"},
                new Room{ RoomID = 5, Building = "NEW YORK"},

            };


        var selectList = new SelectList(list,"RoomID", "Building");
        ViewData["BuildingList"] = selectList;  

        return View();
    }

    //
    // POST: /Room/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Room room)
    {
        if (ModelState.IsValid)
        {
            db.Rooms.Add(room);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(room);
    }
Run Code Online (Sandbox Code Playgroud)

我的看法:

    <div>
        @Html.LabelFor(model => model.Building, "Building")
    </div>
    <div>

        @Html.DropDownList("BuildingList", String.Empty)
        @Html.ValidationMessageFor(model => model.Building)
    </div>
Run Code Online (Sandbox Code Playgroud)

请帮忙...

谢谢。

Dav*_*nce 5

您的下拉菜单已填充吗?鉴于您的代码,我认为您需要执行以下操作:

@Html.DropDownListFor(model => model.Building, ViewData["BuildingList"])
Run Code Online (Sandbox Code Playgroud)

IE。将所选值绑定到Building您的属性Room,并使用视图模型中的下拉列表来填充列表。

我也不确定这是否是您的意图。您用房间填充下拉列表,然后根据您的选择创建一个新房间,这似乎有点可疑。

编辑

好吧,我会让你的事情变得更容易。

我将从你的课程开始。这是我假设您正在使用的房间:

public class Room
{
    public int RoomId { get; set; }
    public string Building { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在让我们做一些比使用 ViewData 更好的事情。我已经为您创建了一个视图模型。SelectedRoomId您将使用您的选择列表填充此列表,并且您在视图中选择的项目将在您发布表单时绑定到 中。

public class ViewModel
{
    public int SelectedRoomId { get; set; }
    public SelectList RoomOptions { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

控制器

private SelectList GetSelectList()
{
    var list = new[] 
    { 
        new Room { RoomId = 1, Building = "FAYARD HALL"},
        new Room { RoomId = 2, Building = "WHATEVER HALL"},
        new Room { RoomId = 3, Building = "TIME SQUARE"},
        new Room { RoomId = 4, Building = "MISSISSIPPI"},
        new Room { RoomId = 5, Building = "NEW YORK"}
    };

    return new SelectList(list, "RoomId", "Building");
}

public ActionResult Create()
{
    ViewModel viewModel = new ViewModel
    {
        RoomOptions = GetSelectList()
    };
    return View(viewModel);
}

[HttpPost]
public ActionResult Create(ViewModel viewModel)
{
    if (ModelState.IsValid)
    {
        // Save here
        // create a new room using the SelectedOptionId in the viewModel
        return RedirectToAction("Index", "Home");
    }
    // repopulate the list if something failed
    viewModel.RoomOptions = GetSelectList();
    return View(viewModel);
}
Run Code Online (Sandbox Code Playgroud)

看法

@model PathToYourViewModel.ViewModel

@using (Html.BeginForm())
{
    @Html.DropDownListFor(model => model.SelectedRoomId, Model.RoomOptions, "-- select an option --")
    <button type="submit">Submit</button>
};
Run Code Online (Sandbox Code Playgroud)

经过尝试和测试。祝你好运!