C# 在每行都有下拉列表的表格中设置初始值

C# 在每行都有下拉列表的表格中设置初始值,c#,asp.net-mvc,C#,Asp.net Mvc,我有一张桌子,上面有一个特定客户的活动注册列表。我在每一行都有下拉列表,允许更改该行中给定事件的客户注册状态。但是,列表中未设置其当前状态;在每个列表中选择第一项 视图模型: public class EditRegistrationViewModel { public int SiteMemberId { get; set; } public string Name { get; set; } public List<EditParticipantViewMode

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

视图模型:

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; }
}
以下是获取选择列表的代码:

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;
}
公共静态列表GetSelectListForConstraintId(int constraintId)
{
CCSContext db=新的CCSContext();
列表列表=新列表();
List enumList=db.Enum.Where(x=>x.nConstraintID==constraintId).OrderBy(x=>x.nOrder.ToList();
foreach(枚举列表中的变量i)
{
SelectListItem=新建SelectListItem()
{
Text=i.t显示名称,
Value=i.nIndex.ToString()
};
列表。添加(ITEEM);
}
返回列表;
}

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

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


提交表单时,模型绑定也会起作用。

循环中的绑定
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" })

只要
SelectedParticipationStatus
中有值,这就应该可以了。你能发布
GetSelectListForConstraintId
吗?@adiga我已经添加了这个方法。本质上我只是循环数据库中的状态列表,从每个状态列表中生成一个selectlistitem,然后返回列表。@adiga是的,这就是让我困惑的地方特别是,因为我的代码中还有其他地方可以做到这一点,只是没有一个列表,其中每行都有一个下拉列表。我只是想知道循环有一个问题。请尝试
@Html.DropDownListFor(m=>m.Participants[I]。SelectedParticipationStatus,new SelectList(Model.Participants[I]。ParticipationStatusList,“值”,“文本”,Model.participations[i].SelectedParticipationStatus),new{@class=“form control”}
@adiga相同的结果。我也将尝试下面的答案。就是这样。现在,我似乎可以了解更多关于EditorTemplates的信息了。
@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>
@model EditRegistrationViewModel
<h2>Form using Editor template</h2>
@using (Html.BeginForm("Index", "Home"))
{
  <table>
    @Html.EditorFor(f=>f.Participants)
  </table>
  <input type="submit" />
}
@Html.DropDownListFor(
        m => m.Participants[i].SelectedParticipationStatus,
        new SelectList(Model.Participants[i].ParticipationStatusList, "Value","Text", 
           Model.Participants[i].SelectedParticipationStatus),
        new { @class = "form-control" })