ASP.NET MVC 2: Disabled TextBox for DateTime returns null

I encounter annoying binding behavior to bind DateTime to a text field that is disabled. It always returns null.

Does my model have a DateTime? Property StartDate ... I also tried only DateTime StartDate (without '?').

I tried the following:

Attempt # 1:

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

Attempt number 2:

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

where DateDisabled is a partial view defined as follows:

<%@ 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 return a null value. Did I miss something? Or a workaround?

+3
source share
1 answer

Attribute input elements disablednever send their value to the server when the form is submitted. Instead, you should use the attribute readonly:

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

Source: https://habr.com/ru/post/1770838/


All Articles