2013-07-11 58 views
0

我的ComponentParameter S(如下圖所示)名單上使用EditorFor。編輯器模板根據值進行切換,並且日期時間不起作用。當我在回發之前查看HTML時,值就在那裏,但在模型內的List<ComponentParameter>中,與日期時間對應的點是空的。MVC隱藏字段不回發

public class ComponentParameter 
{ 
    public string Value { get; set; } 
    public string Description { get; set; } 
    public string Type { get; set; } 
    public bool Optional { get; set; } 
} 

EditorTemplate:

@switch (Model.Type.ToLowerInvariant()) 
{ 
case "datetime": 
     Html.RenderPartial("ParameterHeader", Model); 
<div class="grid_4"> 
    @{ 
     DateTime value; 
     if (!DateTime.TryParse(Model.Value, out value)) 
     { 
      value = DateTime.Today + TimeSpan.FromHours(6); 
     } 
    } 
    // Commenting out the next 2 lines causes the value to post back 
    <div class="grid_9">@Html.TextBox("", value.ToString("MM/d/yyyy"), new { @class = "date-picker autotooltip " + Model.RequiredClass, @data_mwtooltip = Model.Description })</div> 
    @Html.TextBox("", value.ToString("hh:mm tt"), new {@class="time-picker"}) 
    @Html.HiddenFor(x => x.Value, new { @class = "input-time-picker" }) 
    @Html.HiddenFor(x => x.Optional) 
    @Html.HiddenFor(x => x.Description) 
    @Html.HiddenFor(x => x.Type) 
</div> 
     break; 
case "string": 
    <div class="grid_12"> 
     @{ Html.RenderPartial("ParameterHeader", @Model); } 
@Html.TextBoxFor(x => x.Value, new { @class = "autotooltip " + Model.RequiredClass, @data_mwtooltip = Model.Description }) 
@Html.HiddenFor(x => x.Optional) 
@Html.HiddenFor(x => x.Description) 
@Html.HiddenFor(x => x.Type) 
    </div> 
     break; 
} 

HTML:

<div class="grid_4"> 
<div class="grid_9"> 
<span class="ui-spinner ui-widget ui-widget-content ui-corner-all"> 
<input id="ComponentList_0__ComponentParameterList_0_" class="time-picker ui-spinner-input valid" type="text" value="06:00 AM" name="ComponentList[0].ComponentParameterList[0]" aria-valuenow="1357041600000" autocomplete="off" role="spinbutton"> 
<a class="ui-spinner-button ui-spinner-up ui-corner-tr ui-button ui-widget ui-state-default ui-button-text-only" tabindex="-1" role="button" aria-disabled="false"> 
<a class="ui-spinner-button ui-spinner-down ui-corner-br ui-button ui-widget ui-state-default ui-button-text-only" tabindex="-1" role="button" aria-disabled="false"> 
</span> 
<input id="ComponentList_0__ComponentParameterList_0__Value" class="input-time-picker" type="hidden" value="07/11/2013 06:00 AM" name="ComponentList[0].ComponentParameterList[0].Value"> 
<input id="ComponentList_0__ComponentParameterList_0__Optional" type="hidden" value="False" name="ComponentList[0].ComponentParameterList[0].Optional"> 
<input id="ComponentList_0__ComponentParameterList_0__Description" type="hidden" value="Start Time" name="ComponentList[0].ComponentParameterList[0].Description"> 
<input id="ComponentList_0__ComponentParameterList_0__Type" type="hidden" value="datetime" name="ComponentList[0].ComponentParameterList[0].Type"> 
</div> 
<div class="grid_12"> 
<label> 
<input id="ComponentList_0__ComponentParameterList_5__Value" class="autotooltip paramRequired required" type="text" value="" name="ComponentList[0].ComponentParameterList[5].Value" data-mwtooltip="Attention Line" title=""> 
<input id="ComponentList_0__ComponentParameterList_5__Optional" type="hidden" value="False" name="ComponentList[0].ComponentParameterList[5].Optional"> 
<input id="ComponentList_0__ComponentParameterList_5__Description" type="hidden" value="Attention Line" name="ComponentList[0].ComponentParameterList[5].Description"> 
<input id="ComponentList_0__ComponentParameterList_5__Type" type="hidden" value="string" name="ComponentList[0].ComponentParameterList[5].Type"> 
</div> 
+1

我不知道,如果是這種情況,但該指數'[N]'*必須*可以以0 *和*順序,以便模型綁定工作。 –

+0

@Andre:是的,數組中有6個元素,前2個(0和1)用於非工作日期時間,最後4個(顯示在回發中)用於工作字符串和textareas。我只舉了一個例子。 –

+0

對不起,但我覺得有一些代碼缺失。你能否提供你的整個觀點以及接受這個職位的行動的簽名? –

回答

0

再次在HTML看,問題是,HTML輔助試圖綁定到模型爲好,就證明通過id="ComponentList_0__ComponentParameterList_0_"。這似乎導致該模型完全不能綁定,由於錯誤。

因爲我只是想獲得一些輸入字段一起工作,我刪除那些與常規的HTML <input>取而代之。

+0

我看不出爲什麼'id'可能是問題所在。但是,如果它解決了你的問題,那就走。 –

+0

這不是一個合適的解決方案。問題是您正在使用嵌套模型。您仍然可以使用Html Helpers,但是您需要在控制器操作中使用正確的前綴,以便綁定起作用。不幸的是,我無法幫助你,因爲你還沒有發佈你的整個視圖。至少,我需要知道你傳遞給你的視圖的模型,以便我能更好地幫助你。 – ataravati

+0

@ataravati試圖綁定不存在的事物的模型的一個適當的解決方案不是爲了讓模型不嘗試綁定那些不存在的事物?嵌套模型是問題嗎?我們在解決方案的其他地方使用它們。如果你不應該使用嵌套模型,他們爲什麼會提供EditorTemplates?也許我甚至不需要一個盒子,我們不需要這些東西的價值,但那不像你說的那樣。 –