2017-09-18 34 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" })