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

时间:2013-09-08 00:36:17

标签: 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);
    }

我的观点:

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

        @Html.DropDownList("BuildingList", String.Empty)
        @Html.ValidationMessageFor(model => model.Building)
    </div>

请帮忙......

谢谢。

2 个答案:

答案 0 :(得分:4)

您的下拉是否已填充?鉴于您的代码,我认为您需要以下内容:

@Html.DropDownListFor(model => model.Building, ViewData["BuildingList"])

即。将所选值绑定到Building的{​​{1}}属性,并使用视图模型中的下拉列表填充列表。

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

修改

好的,我会让你的事情变得更轻松。

我将从你的课程开始。这是我假设您正在与之合作的房间:

Room

现在让我们做一些比使用ViewData更好的事情。我为你创建了一个视图模型。您将使用选择列表填充此内容,并且当您发布表单时,您在视图中选择的项目将绑定到public class Room { public int RoomId { get; set; } public string Building { get; set; } }

SelectedRoomId

<强>控制器

public class ViewModel
{
    public int SelectedRoomId { get; set; }
    public SelectList RoomOptions { get; set; }
}

查看

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

试过并测试过。祝你好运!

答案 1 :(得分:1)

模型绑定是在mvc。{/ p>中的names property的帮助下进行的

在您的情况下,您的控件名称为BuildingList

@Html.DropDownList("BuildingList", (SelectList)ViewData["BuildingList"])

因此,在您的控制器上,操作将如下所示:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(FormCollection collection)
{
   var selectedValue = collection["BuildingList"];
}