【问题标题】:Set initial value in a table with a dropdown on each row [duplicate]在表格中设置初始值,每行都有一个下拉列表[重复]
【发布时间】:2017-09-18 18:52:07
【问题描述】:

我有一张表格,其中列出了特定客户的活动注册列表。我在每一行都有下拉菜单,允许更改该行中给定事件的客户注册状态。但是,它们的当前状态并未在列表中设置;在每个列表中选择第一项。

视图模型:

public class EditRegistrationViewModel
{
    public int SiteMemberId { get; set; }
    public string Name { get; set; }
    public List<EditParticipantViewModel> Participants { get; set; }
}

public class EditParticipantViewModel
{
    public int ParticipantId { get; set; }

    [Display(Name = "Event ID")]
    public int EventId { get; set; }

    [Display(Name = "Name")]
    public string Name { get; set; }

    [Display(Name = "Start Date")]
    public string StartDateTime { get; set; }

    [Display(Name = "Participation Status")]
    public int SelectedParticipationStatus { get; set; }
    public List<SelectListItem> ParticipationStatusList { get; set; }
}

查看(只是 C# for 循环块)

@for (int i = 0; i < Model.Participants.Count; i++)
{
    <tr>
        @Html.HiddenFor(m => m.Participants[i].ParticipantId)
        <td class="text-right">@Model.Participants[i].EventId</td>
        <td>@Model.Participants[i].Name</td>
        <td>@Model.Participants[i].StartDateTime</td>
        <td>@Html.DropDownListFor(
            m => m.Participants[i].SelectedParticipationStatus,
            Model.Participants[i].ParticipationStatusList,
            new { @class = "form-control" })</td>
    </tr>
}

控制器(填充初始值,但下拉菜单未设置)

foreach (var i in participantQuery)
{
    EditParticipantViewModel theItem = new EditParticipantViewModel()
    {
        ParticipationStatusList = tblEnum.GetSelectListForConstraintId(CRConstants.PARTICIPATION_STATUS_CONSTRAINT_ID),
        ParticipantId = i.a.aID,
        EventId = i.c.aID,
        Name = i.c.tName,
        SelectedParticipationStatus = i.b.nParticipationStatus ?? 0
    };
    DateTime? startDate = i.d.GetStartDateTime();
    theItem.StartDateTime = startDate.HasValue ? startDate.Value.ToString() : "-";
    theViewModel.Participants.Add(theItem);
}

这是获取选择列表的代码:

public static List<SelectListItem> GetSelectListForConstraintId(int constraintId)
{
    CCSContext db = new CCSContext();
    List<SelectListItem> theList = new List<SelectListItem>();
    List<tblEnum> enumList = db.Enum.Where(x=>x.nConstraintID == constraintId).OrderBy(x=>x.nOrder).ToList();
    foreach (var i in enumList)
    {
        SelectListItem theItem = new SelectListItem()
        {
            Text = i.tDisplayName,
            Value = i.nIndex.ToString()
        };
        theList.Add(theItem);
    }
    return theList;
}

【问题讨论】:

  • 只要SelectedParticipationStatus 中存在价值,这应该可以工作。您也可以发布GetSelectListForConstraintId 吗?
  • @adiga 我已经添加了方法。本质上,我只是遍历数据库中的状态列表,从每个状态中创建一个 selectlistitem,然后返回列表。
  • @adiga 是的,这尤其让我感到困惑,因为我的代码中有其他地方可以做到这一点,而不是每行都有下拉列表的列表。
  • 我只记得循环存在问题。试试@Html.DropDownListFor( m =&gt; m.Participants[i].SelectedParticipationStatus, new SelectList(Model.Participants[i].ParticipationStatusList, "Value","Text", Model.Participants[i].SelectedParticipationStatus ), new { @class = "form-control" })
  • @adiga 结果相同。我也将在下面尝试这个答案。

标签: c# asp.net-mvc


【解决方案1】:

当您必须在循环中使用 html 辅助方法来呈现输入元素并且您希望模型绑定完美地工作时,最好使用 EditorTemplates。这样您就不需要编写大量代码循环/操作输入元素名称等。

只需在~\Views\Shared\ 或~\Views\YourControllerName 下创建一个名为EditorTemplates 的新文件夹,并创建一个与您用作集合项类型的类名称(EditParticipantViewModel.cshtml)同名的视图

有下面的代码来呈现每一行

@model EditParticipantViewModel
<tr>
    <td class="text-right">@Model.EventId @Html.HiddenFor(m => m.ParticipantId)</td>
    <td>@Model.Name</td>
    <td>
        @Html.DropDownListFor(
                        m => m.SelectedParticipationStatus,
                        Model.ParticipationStatusList, new { @class = "form-control" })
    </td>
</tr>

现在在强类型为EditRegistrationViewModel 的主视图中,调用Html.EditorFor 辅助方法

@model EditRegistrationViewModel
<h2>Form using Editor template</h2>
@using (Html.BeginForm("Index", "Home"))
{
  <table>
    @Html.EditorFor(f=>f.Participants)
  </table>
  <input type="submit" />
}

这将为您的视图模型的 Participants 集合中的每个项目呈现表格行,并且对于每一行,将预先选择所需的选项(假设您正在为每个项目设置正确的 SelectedParticipationStatus 属性值GET 操作,它与选项的 value 属性值之一匹配。)

模型绑定也将在您提交表单时起作用。

【讨论】:

  • 就是这样。现在看来我可以了解有关 EditorTemplates 的更多信息了。
【解决方案2】:

在循环中绑定DropDownListFor 存在问题。你可以使用EditorTemplates,也可以试试这样的:

@Html.DropDownListFor(
        m => m.Participants[i].SelectedParticipationStatus,
        new SelectList(Model.Participants[i].ParticipationStatusList, "Value","Text", 
           Model.Participants[i].SelectedParticipationStatus),
        new { @class = "form-control" })

【讨论】:

    猜你喜欢
    • 2021-01-11
    • 2018-03-05
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    • 2018-04-19
    • 2011-03-19
    • 2011-02-27
    • 1970-01-01
    相关资源
    最近更新 更多