gpt4 book ai didi

asp.net-mvc-2 - ASP.NET MVC 2 : Disabled TextBox for DateTime returns null

转载 作者:行者123 更新时间:2023-12-01 23:03:19 25 4
gpt4 key购买 nike

我遇到了绑定(bind)到已禁用文本框的 DateTime 的令人讨厌的绑定(bind)行为。它总是返回 null。

我的模型有日期时间? StartDate 属性...我也尝试过 DateTime StartDate (没有'?')。

我尝试了以下方法:

尝试#1:

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

尝试#2:
<%: Html.EditorFor(model => model.StartDate, "DateDisabled")%>

其中 DateDisabled 是这样定义的局部 View :
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime?>" %>
<%: Html.TextBox("", Model.HasValue ? Model.Value.ToShortDateString() : "", new { @class = "text-box-short-disabled", @disabled = "true" })%>

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

最佳答案

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

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

关于asp.net-mvc-2 - ASP.NET MVC 2 : Disabled TextBox for DateTime returns null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3992995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com