0

我正在尝试将 DropDownList 的选定值传递给新的控制器方法。但是,在控制器中,leagueKey总是返回 null。下拉列表中填充了值。

如果我更改new { leagueKey = Model.SelectedLeagueKey}new {leagueKey = "test"}控制器正确接收“测试”值。看来 DropDownList 没有将所选值绑定到Model.SelectedLeagueKey.

模型

public Dictionary<string, string> Leagues { get; set; }
public string SelectedLeagueKey { get; set; }

看法

<div class="edit-field">
    @Html.DropDownListFor(model => model.SelectedLeagueKey, new SelectList(Model.Leagues, "Key", "Value", Model.SelectedLeagueKey),"Select League")
</div>
    @Html.ActionLink("Select League", "AddTeam", "Team", new { leagueKey =  Model.SelectedLeagueKey}, null)

控制器

public ActionResult AddTeam(LTEDContext context, string leagueKey)
    {
        //Do something with leagueKey here
        return View();
    }
4

1 回答 1

3

您的视图根据以下方式向您发送 SelectedLeagueKey 参数

<div class="edit-field">
   @Html.DropDownListFor(model => model.SelectedLeagueKey, new SelectList(Model.Leagues, "Key", "Value", Model.SelectedLeagueKey),"Select League")
</div>

尝试在视图中使用下一个代码:

@using (Html.BeginForm("AddTeam", "Team")) {    
<div class="edit-field">
   @Html.DropDownListFor(model => model.SelectedLeagueKey, new SelectList(Model.Leagues, "Key", "Value", Model.SelectedLeagueKey),"Select League")
</div>
<submit type="submit"/>

}

和控制器中的下一个:

public ActionResult AddTeam(LTEDContext context, string SelectedLeagueKey)
{
    //Do something with leagueKey here
    return View();
}
于 2012-12-18T02:31:10.247 回答