2017-09-18 31 views
0

我有一个表格,其中包含特定客户的事件注册列表。每行都有下拉列表,允许更改该行中给定事件的客户注册状态。但是,他们目前的状况并未列入清单;每个列表中都会选择第一个项目。在表格中设置每行下拉的初始值

视图模型:

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; 
} 
+0

只要有一个在'SelectedParticipationStatus'价值,这应该work.Could您发布'GetSelectListForConstraintId'呢? – adiga

+0

@adiga我已经添加了该方法。本质上,我只是循环访问数据库中的状态列表,从每个状态中创建一个selectlistitem,然后返回列表。 – benjsigmon

+0

@adiga是的,这就是让我特别困惑的地方,因为我的代码中有其他地方可以做到这一点,只是不是每行都有下拉列表的列表。 – benjsigmon

回答

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属性值中的一个相匹配。)

当您提交表单时,模型绑定也可以使用。

+1

就是这样。现在看起来我可以更多地了解EditorTemplates。 – benjsigmon

0

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