2013-07-11 55 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?也许我甚至不需要一个盒子,我们不需要这些东西的价值,但那不像你说的那样。 –