Search code examples
jquery.netasp.net-mvc-3permissionsform-helpers

MVC3 changes not saving when controlled disabled


I'm trying to disable MVC3 controls dynamically based on the user's permission. It works fine and automatically sets to a default value when changes are made. But when saving after clicking the submit button none of the changes are saved. This only happens when the control is disabled.

*UPDATE*

<div class="editor-field">
                    @if (!disabled)
                    {

                        @Html.TextBox("EffectiveDate", Model.EffectiveDate.FormatShortDate(), new { @class = "date" })
                    }
                    else
                    {
                        @Html.TextBox("ReadOnlyEffectiveDate", Model.EffectiveDate.FormatShortDate(), new { @class = "date", @disabled = "disabled" })
                        @Html.HiddenFor(m => m.EffectiveDate)
                    }
                    @Html.ValidationMessageFor(model => model.EffectiveDate)
                </div>

jqueryscript

$("#DistributionId").change(function () {
            var currentYear = (new Date).getFullYear();
            $("#TermDate").val("12/31" + currentYear);
            $("#EffectiveDate").val("01/01/" + (currentYear + 1));
            $("#ReadOnlyEffectiveDate").val("01/01/" + (currentYear + 1));
            $("#EffectiveDate").change(); 
        });

the value for #ReadOnlyEffectiveDate does not update #EffectiveDate does but saving still doesn't work.


Solution

  • Disabled fields do not get posted back. If you needed to see which controls are disabled you could use JQuery to enable them and then post them back and then disable them again in your form.