日期没有通过编辑视图我如何在asp.net mvc中传递它

时间:2014-12-12 14:42:11

标签: asp.net-mvc asp.net-mvc-4

----我的控制器---

    public ActionResult Edit(int id)
    {
        var dbContext = new MyDBDataContext();
        var userDetails = dbContext.Users.FirstOrDefault(userId => userId.UserId == id);
        var user = new Management.Models.User();

        if (userDetails != null)
        { 
            user.DOB = Convert.ToDateTime(userDetails.DOB);
            user.POS = Convert.ToDateTime( userDetails.POS); 
        }
        return View(user);
    }

----- ------模型

    [Required(ErrorMessage = "Please Enter the Date that Start service")]
    [Display(Name = "Period Of Service")]
    //[DisplayFormat(DataFormatString = "{dd-mm-yyyy}", ApplyFormatInEditMode = true)]
    [DataType(DataType.Date)]
    public DateTime POS { get; set; }

    [Required(ErrorMessage = "Please Select the Date of birth")]
    [Display(Name = "Date of birth")]
    //[DisplayFormat(DataFormatString = "{dd-mm-yyyy}", ApplyFormatInEditMode = true)]
    [DataType(DataType.Date)]
    public DateTime DOB { get; set; }

---------查看-------

    <div class="editor-label">
        @Html.DisplayName("Date Of Join")
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.POS)
        @Html.ValidationMessageFor(model => model.POS)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Salary)
    </div>

1 个答案:

答案 0 :(得分:0)

如果您使用EditorFor生成浏览器datepicker(HTML5),那么(请注意格式字符串)

Required(ErrorMessage = "Please Enter the Date that Start service")]
[Display(Name = "Period Of Service")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime POS { get; set; }

POS的值将显示在浏览器的文化中。