1

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

我的代码如下:

控制器:

    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>

请帮忙...

谢谢你。

4

2 回答 2

4

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

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

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

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

编辑

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

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

public class Room
{
    public int RoomId { get; set; }
    public string Building { get; set; }
}

现在让我们做一些比使用 ViewData 更好的事情。我已经为你创建了一个视图模型。您将使用您的选择列表填充它,并且您在视图中选择的项目将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);
}

看法

@model PathToYourViewModel.ViewModel

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

尝试和测试。祝你好运!

于 2013-09-08T00:43:09.783 回答
1

names property模型绑定在in mvc的帮助下进行。

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

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

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

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(FormCollection collection)
{
   var selectedValue = collection["BuildingList"];
}
于 2013-09-08T04:55:42.770 回答