javascript - 将 Date 对象从 javascript 传递到 asp.net mvc Controller

标签 javascript asp.net-mvc date

我在客户端有一个日期变量,我想将日期变量传递到服务器端的 Controller 。 我已作为普通字段通过,日期默认为 01-01-0001 12:00:00 AM。

帮我将日期字段转换为正确的格式。

最佳答案

ASP.NET MVC 期望 DateTime 值采用 Thread.CurrentLanguage 格式。请检查您使用的语言。

就像这样,因为用户可能会在文本框中输入日期,然后他们会输入其语言的格式。

就像 Pieter 所说:在这种情况下,一个简单的方法是使用字符串。

另一种方法是使用

protected void Application_BeginRequest(object sender, EventArgs e)
{
    Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
}

在 Global.Asax 中。

模型绑定(bind)发生后,您可以在过滤器中切换回用户的语言:

    public override void OnActionExecuting(ActionExecutingContext filterContext)
     {
             string language = //find a way to get the language - I have it in the route
             Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(language);
             Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
         base.OnActionExecuting(filterContext)
     }

这种方式有一些风险,但我发现如果 modelBinding 使用 InvariantCulture,在很多情况下会更容易(考虑 route 的小数值、 route 的日期时间...)

关于javascript - 将 Date 对象从 javascript 传递到 asp.net mvc Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2927290/

相关文章:

asp.net - 如果我正在使用 ASP MVC 3,我应该学习哪些新技术?

c# - 参数字典包含不可为空类型 'idd' 的参数 'System.Int32' 的空条目

javascript - 损坏的 v8 日期解析器有什么解决方法吗?

javascript - on 不像 jQuery 中的 live 那样处理事件

javascript - 通过向其添加类将 setAttribute 用于 ID

javascript - 使用 Redux 和 React 按顺序执行异步操作

c# - .NET 核心 MvcHtmlString(TextboxFor, LabelFor).ToString() 错误

date - 如何使用 vbscript 将日期从 yyyymmdd 格式化为 mm/dd/yyyy?

java - JX日期选择器 :unpearsable date

javascript - 检查 JSON 对象是否为 null 的快捷方式