Kyle W
Kyle W

Reputation: 3752

MVC hidden fields not posting back

I am using an EditorFor on a list of ComponentParameters (shown below). The editor template switches based on the value, and the datetime isn't working. When I look at the HTML before postback, the values are there, but in the List<ComponentParameter> inside the model, the spots corresponding to the datetimes are null.

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>

Upvotes: 1

Views: 3125

Answers (1)

Kyle W
Kyle W

Reputation: 3752

Looking at the HTML again, the issue is that the Html helpers were trying to bind to the model as well, as evidenced by id="ComponentList_0__ComponentParameterList_0_". This seemed to cause that model to not bind at all, due to the errors.

Since I was only trying to get some input fields to work with, I removed those and replaced them with regular HTML <input>.

Upvotes: 1

Related Questions