TextBoxFor帮手混入日期日和月 [英] TextBoxFor helper mixes day and month in dates

查看:85
本文介绍了TextBoxFor帮手混入日期日和月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑下面的例子:


  1. 视图模型

  1. view model

public class FooViewModel
{
    public DateTime Date { get; set; }
}


  • 控制器

  • controller

    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index(FooViewModel foo)
        {
            return View(foo);
        }
        [HttpPost]
        public ActionResult Submit(FooViewModel foo)
        {
            return RedirectToAction("Index", foo);
        }
    }
    


  • 查看

  • view

    @model MvcApplication1.Models.FooViewModel
    <h1>@Model.Date</h1>
    @using (Html.BeginForm("Submit", "Home", FormMethod.Post))
    {
        @Html.TextBoxFor(m => m.Date)
        <input type="submit" value"Submit" />
    }
    


  • 路线

  • routes

    routes.MapRoute(
        null,
        "",
        new { controller = "Home", action = "Index" }
    );
    routes.MapRoute(
        null,
        "{action}",
        new { controller = "Home" },
        new { action = "Submit" }
    );
    


  • 问题是,表单提交的日期后,编辑得到的值与日和月开机:

    The problem is that after form submit the date editor gets values with day and month switched:




    我能做些什么,以使其正常工作?

    其实我试图格式化编辑器的值:

    Actually I tried to format the value of the editor:


    1. @ Html.TextBoxFor(M = GT; m.Date,新{值= Model.Date.ToString(DD.MM.YYYY)})

    2. 使用的编辑模板的,如图<一个href=\"http://geekswithblogs.net/michelotti/archive/2010/02/05/mvc-2-editor-template-with-datetime.aspx\"相对=nofollow> MVC 2编辑器的模板与日期时间,仅当然采用MVC3;我还用 EditorFor 帮手,而不是使可利用的模板。

    3. 使用 DataAnnotations [DisplayFormat(ApplyFormatInEditMode = TRUE,DataFormatString ={0:MMM / DD / YYYY})]

    1. @Html.TextBoxFor(m => m.Date, new { value = Model.Date.ToString("dd.MM.yyyy") })
    2. using editor templates, as shown in MVC 2 Editor Template with DateTime, only of course adopted to MVC3; and I also used EditorFor helper instead to make the template be utilized.
    3. using DataAnnotations: [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MMM/dd/yyyy}")]

    这些改变什么都不是。无论是编辑器的值发生变化,也不出现格式化。

    None of these changed anything. Neither the value of the editor changed, nor it appeared formatted.

    在最后,我(后面的答案 DrJokepu )进行管理,使这项工作只能用原始的HTML,即对于没有helper方法的DateTime

    public static MvcHtmlString GetDateEditor(this FooViewModel model)
    {
        string temp = "<input id=\"Date\" type=\"text\" value=\"{0}\" name=\"Date\" 
                        data-val-required=\"Please, enter a valid date\" 
                        data-val=\"true\" />";
        return MvcHtmlString.Create(
                     string.Format(temp, 
                                   model == null 
                                        ? string.Empty 
                                        : model.Date.ToString("dd.MM.yyyy")));
        }
    

    如果有一种方式来实现与任何辅助方法相同的结果仍然是一个秘密,我...

    If there is a way to achieve the same result with any of the helper methods remains a secret to me...

    推荐答案

    请尝试些许变化版本:结果
    @ Html.TextBoxFor(M = GT; m.Date,新{@Value = Model.Date.ToString(DD.MM.YYYY)})

    Please, try slightly changed version:
    @Html.TextBoxFor(m => m.Date, new { @Value = Model.Date.ToString("dd.MM.yyyy") })

    这篇关于TextBoxFor帮手混入日期日和月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆