views:

20

answers:

1

I'm encountering an irritating binding behavior for a DateTime bound to a textbox that is disabled. It always returns null.

My model has a DateTime? StartDate property... I've also tried just DateTime StartDate (without the '?').

I've tried the following:

Attempt #1:

<%: Html.TextBoxFor(model => model.StartDate, new { @disabled="true" })%>

Attempt #2:

<%: Html.EditorFor(model => model.StartDate, "DateDisabled")%>

where DateDisabled is a partial view defined like this:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime?>" %>
<%: Html.TextBox("", Model.HasValue ? Model.Value.ToShortDateString() : "", new { @class = "text-box-short-disabled", @disabled = "true" })%>

All my attempts returns a null value. Is there something I missed? Or a workaround?

+1  A: 

Input elements with the disabled attribute never send their value to the server when the form is submitted. You should use the readonly attribute instead:

<%: Html.TextBoxFor(model => model.StartDate, new { @readonly = "readonly" }) %>
Darin Dimitrov