2017-07-17 73 views
-1

我目前在瀏覽器中編輯集合時遇到問題。我想編輯一個視圖模型,它有3個屬性,一個int,一個字符串和一個項目列表。Razor MVC 5 - 編輯包含枚舉的項目集合

下面的代碼:

public class HospitalDoctorAttributions { 
    public string HospitalID { get; set; } 
    public int Year { get; set; } 
    public List<AmountOfWork> Attributions { get; set; } 
} 

public class AmountOfWork { 
    public WorkAreaEnum Sector { get; set; } 
    public decimal FullTimeEquivalent { get; set; } 
    public string Comment { get; set; } 
} 

我想編輯的工作部門的歸屬,所以我做了這個編輯器模板:

@model AmountOfWork 

@Html.HiddenFor(model => model.Sector) 

<tr> 
    <td>@Model.Sector.GetDisplayName()</td> 
    <td>@Html.EditorFor(model => model.FullTimeEquivalent)</td> 
    <td>@Html.EditorFor(model => model.Comment)</td> 
</tr> 

這是由視圖refered:

@model HospitalDoctorAttribution 

// [...] Editors for the first two properties in the model 

<table class="table table-bordered"> 
    <tr> 
     <th>Work Sector</th> 
     <th>Amount by FTE</th> 
     <th>Comment (optional)</th> 
    </tr> 
    @for (var i = 0; i < Model.Attributions.Count; i++) 
    { 
     @Html.EditorFor(x => x.Attributions[i]) 
    } 
</table> 

當我打開視圖時,AmountOfWork項目的屬性被正確映射,但是當我編輯它們時,它會梨沒有任何東西回到控制器。然而,Year和HospitalID已成功發佈回控制器。

如何解決這個問題?

編輯:生成的HTML代碼

<tr> 
    <input Value="0" data-val="true" data-val-required="Le champ Sector est requis." id="Attributions_0__Sector" name="Attributions[0].Sector" type="hidden" value="URGENCE" /> 
    <td>Urgence</td> 
    <td><input class="text-box single-line" data-val="true" data-val-number="Le champ FullTimeEquivalent doit être un nombre." data-val-required="Le champ FullTimeEquivalent est requis." id="Attributions_0__FullTimeEquivalent" name="Attributions[0].FullTimeEquivalent" style="display:table-cell; width:100%" type="text" value="0,00" /></td> 
    <td><input class="text-box single-line" id="Attributions_0__Comment" name="Attributions[0].Comment" style="display:table-cell; width:100%" type="text" value="" /></td> 
</tr> 
<tr> 
    <input Value="1" data-val="true" data-val-required="Le champ Sector est requis." id="Attributions_1__Sector" name="Attributions[1].Sector" type="hidden" value="GERIARTRIE" /> 
    <td>Gériatrie</td> 
    <td><input class="text-box single-line" data-val="true" data-val-number="Le champ FullTimeEquivalent doit être un nombre." data-val-required="Le champ FullTimeEquivalent est requis." id="Attributions_1__FullTimeEquivalent" name="Attributions[1].FullTimeEquivalent" style="display:table-cell; width:100%" type="text" value="0,00" /></td> 
    <td><input class="text-box single-line" id="Attributions_1__Comment" name="Attributions[1].Comment" style="display:table-cell; width:100%" type="text" value="" /></td> 
</tr> 
+0

您可以將生成的HTML添加到您的問題中嗎? – krillgar

+2

複雜類型(MVC)列表的[EditorFor())的可能的重複](https://stackoverflow.com/questions/20278362/editorfor-for-a-a-list-of-complex-type-mvc) – Alexander

+0

@krillgar完成了! – AlexandreTremblay

回答

0

MVC應該自動索引使用編輯模板,所以這將是收集只是編輯,模型綁定將適用循環和名稱指數的特性:

@Html.EditorFor(x => x.Attributions, "Name of the editor for Single Item if not the same as DataType")