【问题标题】:Getting the selected value from a DropDownListFor using a ViewBag list使用 ViewBag 列表从 DropDownListFor 获取所选值
【发布时间】:2018-05-10 17:44:29
【问题描述】:

我在模型中使用 ViewBag 列表从 DropDownListFor 获取数据时遇到问题。这是我的控制器代码:

[HttpGet]
public ActionResult JoinTeam()
{
    var TeamList = _db.TeamModels.ToList();
    SelectList list = new SelectList(TeamList, "Id", "TeamName");
    ViewBag.TeamList = list;

    return View();
}

Razor 视图表单如下所示:

@using (Html.BeginForm("JoinTeam", "Home", FormMethod.Post))
{
    @Html.TextBoxFor(m => m.DisplayName, new { @class = "form-control form-control-lg", placeholder = "Enter your Battle Net ID" })
    <br/>

    @Html.DropDownListFor(m => m.TeamModel, (SelectList)ViewBag.TeamList, "- Select a Team to Join -", new { @class= "form-control form-control-lg" })
    <br />
    <button type="submit" class="btn btn-primary" style="width:100%;text-align:center;">Submit</button>
}

TextBoxFor 助手正在正确返回数据,但是我在下拉列表中选择的任何选项都不会传递到我的 post 方法中。有人有什么想法吗?

post 操作确实有效,因为它从模型中获取数据以获取 TextBoxFor 帮助,但它看起来像这样:

        [HttpPost]
    public async Task<ActionResult> JoinTeam(GuardianModel model)
    {            

        try
        {
            string BNETId = model.DisplayName.Replace("#", "%23");
            long memberId = 0;
            if (ModelState.IsValid)
            {
                Bungie.Responses.SearchPlayersResponse member = await service.SearchPlayers(MembershipType.Blizzard, BNETId);
                memberId = member[0].MembershipId;
            }
            using (var context = new CoCodbEntities1())
            {
                var g = new GuardianModel
                {
                    MembershipId = memberId.ToString(),
                    DisplayName = BNETId,
                    MembershipType = 4,
                    TeamID = model.TeamModel.Id
                };
                TempData["UserMessage"] = ViewBag.TeamList.Id;
                return RedirectToAction("Success");
            }
        }
        catch
        {

        }

        return View();
    }

These are the values getting passed into the Post action

【问题讨论】:

  • 您的帖子操作是什么样的?您的 post 请求的内容是什么(浏览器的调试器会显示给您)?
  • 我用发布操作更新了我原来的问题。谢谢。
  • 使用您共享的代码,模型绑定器应该能够将选定的选项值映射到您的 http post 操作方法的 GuardianModel 参数的 TeamModel 属性。你 100% 确定它没有被填充吗? (你能在代码中放一个断点并检查值吗?)
  • 我用传递给 post 操作的值的片段更新了问题。

标签: c# asp.net-mvc


【解决方案1】:

从您分享的屏幕截图来看,TeamModel 属性似乎是TeamModel 类型的虚拟导航属性。你不应该为加载它而烦恼。您只需要担心加载外键属性值(通常是像int 这样的简单类型。

您的 SELECT 元素名称应为 TeamID。提交表单时,它会将选定的选项值映射到模型的TeamID 属性值,即外键属性。

@Html.DropDownListFor(m => m.TeamID, (SelectList)ViewBag.TeamList,
           "- Select a Team to Join -", new { @class= "form-control form-control-lg" })

虽然这可能会解决问题,但最好使用视图模型而不是使用实体类。

【讨论】:

  • 您应该考虑使用视图模型。
  • 不使用 ViewBag?我有理由这样做吗?
  • 是的。 ViewBag 是动态的。所以如果你打错了,它不会在编译时被发现。此外,您不应该让您的视图层与您的 ORM 实体强耦合。
【解决方案2】:

我发现了我遇到的问题。我需要传递到 post 操作中的只是 TeamModel 的 ID。所以我改变了这一行:

@Html.DropDownListFor(m => m.TeamModel.Id, (SelectList)ViewBag.TeamList, "- Select a Team to Join -", new { @class= "form-control form-control-lg" })

我刚刚添加了 ID,它似乎有效。

【讨论】:

    猜你喜欢
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    相关资源
    最近更新 更多