【问题标题】:dropdownlistfor within a 'for' loop not posting values'for' 循环中的 dropdownlistfor 不发布值
【发布时间】:2016-05-02 00:05:22
【问题描述】:

我有多个使用 for 循环呈现的下拉列表,我在让它们将所选值发布到控制器时遇到问题。在我的查询中,我的选择列表是这样的:

model.CreateGroupForm.Genders = new List<SelectListItem>
            {
                 new SelectListItem() {Text = "Either", Value = "Either"},
                new SelectListItem() {Text = "Male", Value = "Male"},
                new SelectListItem() {Text = "Female", Value = "Female"},
            };

我的第一个问题甚至是让我的下拉菜单显示数据库值,尽管我确认它正在检索正确的值。它不适用于此:

@for (var c = 0; c < Model.ExistingGroups.Count; c++)
     {
      @using (Html.BeginForm("EditGroup", "Group", new { id = Model.Id.StripCollectionName(), slug = Model.Slug, innerid = Model.ExistingGroups[c].Id }, FormMethod.Post, new { id = "editcommunityteamform" + c.ToString(CultureInfo.InvariantCulture), @class = "nomarginbottom" }))                                    
      {
      ...
      @Html.DropDownListFor(x => x.ExistingGroups[c].Gender, Model.Createform.Genders)

     <button type="submit" class="btn btn-primary" title="Update name and description of this group">Update</button>
      }
     }

在对 Stack 进行了一些挖掘之后,我发现呈现的每个下拉列表都需要它自己的单独列表。所以我把它改成:

 @Html.DropDownListFor(x => x.ExistingGroups[c].Gender,
new SelectList( Model.CreateGroupForm.Genders,"Value", "Text",Model.ExistingGroups[c].Gender))

这会正确显示查询的值,但是当我提交表单时它只是将 null 发布到控制器。我在 for 循环中使用 checkboxfor boolean 时遇到了同样的问题。

控制器中的我的 ActionResult 只需要一个字符串值,如下所示:

public ActionResult EditGroup(EditGroupInput input)
    {
      var command = new EditGroupCommand(input.Gender);
      ....

我的视图模型如下所示:

public IList<CommunityGroup> ExistingGroups { get; set; }
public CreateGroupInput CreateGroupForm { get; set; }

然后上面2个类就有了代码中提到的属性。

【问题讨论】:

  • 当您说发布 null 时,您是否在发布操作中使用相同的织机来获取值。
  • 不,actionResult中没有循环,应该有吗?我已经用我目前拥有的内容编辑了 OP。
  • 这看起来像 ASP.NET MVC 和 Razor?可能想要添加这些标签,以便吸引熟悉这些技术的人的注意。
  • 你需要有相同的循环,这样你才能得到 x.ExistingGroups[c].Gender
  • 我真的不明白为什么。提交按钮应该只是发布所选下拉列表的值。每个下拉列表都有一个单独的提交按钮,因此每个按钮应该只发布与其关联的下拉列表的值。

标签: c# asp.net-mvc model-view-controller razor


【解决方案1】:

我发现了问题,就是 dropdownlistfor、checkboxlistfor 等不喜欢在“for”循环中操作。我当然不具备理解原因的技术知识,但是当我将 dropdownlistfor 更改为 dropdownlist 时,它起作用了。所以解决方案是这样的:

@for (var c = 0; c < Model.ExistingGroups.Count; c++)
     {
      @using (Html.BeginForm("EditGroup", "Group", new { id = Model.Id.StripCollectionName(), slug = Model.Slug, innerid = Model.ExistingGroups[c].Id }, FormMethod.Post, new { id = "editcommunityteamform" + c.ToString(CultureInfo.InvariantCulture), @class = "nomarginbottom" }))                                    
      {
      ...
      @Html.DropDownList("Gender",  new SelectList(Model.CreateGroupForm.Genders, "Value", "Text", Model.ExistingGroups[c].Gender))
     ...
      }
     }

【讨论】:

  • DropDownListFor()for 循环中工作得非常好(并且使用非强类型DropDownList() 是一种糟糕的做法)。您的问题是您没有发回您在视图中定义的相同模型。但是无论如何这都没有任何意义——为什么你有多个表单在一个循环中——你一次只能回发一个表单
  • Stephen 这是一个编辑页面,每个组都有一行,每行末尾都有一个更新按钮。这使用户能够更改他想要的任何组。我知道我可以在最后使用一个提交按钮来完成,但这就是客户想要的方式。这就是页面上有多个表单的原因。我已经用 viewmodel 编辑了 OP,因为我很想了解为什么我不能让它与 dropdownlistfor 一起工作。
  • 因为@Html.DropDownListFor(x =&gt; x.ExistingGroups[c].Gender, ...) 生成的&lt;select name="ExistingGroups[#].Gender" ... &gt; 与您发回的模型无关(必须是单个对象,而不是集合)
  • 我明白了,所以由于我无法将集合发布回控制器,我想我只能使用下拉列表,没有完全重新设计。
【解决方案2】:

EditGroupInput 应该是 ExistingGroups 的集合,因为控制器操作方法与视图强绑定。或者使用 formcollection 作为参数,查看从视图发布到控制器的所有键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 2016-12-27
    相关资源
    最近更新 更多