ASP.NET MVC 2:DateTime的禁用TextBox返回null

Tom*_*Tom 3 asp.net-mvc-2

我遇到了绑定到禁用的文本框的DateTime的恼人绑定行为.它总是返回null.

我的模型有DateTime?StartDate属性......我也尝试过DateTime StartDate(没有'?').

我尝试过以下方法:

尝试#1:

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

尝试#2:

<%: Html.EditorFor(model => model.StartDate, "DateDisabled")%>
Run Code Online (Sandbox Code Playgroud)

其中DateDisabled是这样定义的局部视图:

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

我的所有尝试都返回一个空值.我错过了什么吗?还是一个解决方法?

Dar*_*rov 11

具有该disabled属性的输入元素在提交表单时永远不会将其值发送到服务器.您应该使用该readonly属性:

<%: Html.TextBoxFor(model => model.StartDate, new { @readonly = "readonly" }) %>
Run Code Online (Sandbox Code Playgroud)