-1

我有 html 作为

@Html.EditorFor(model => model.DateOfBirth})

和财产作为

    [DataType(DataType.Date, ErrorMessage = "Invaild date.")]
    [Display(Name = "Date of birth")]
    public DateTime DateOfBirth { get; set; }

控制器代码如下

 public ActionResult Edit(int id)
    {
        var member = (MembersViewModel)_db.Members.Single(f => f.Id == id);

        return View(member);
    }

模型为

 public class MembersViewModel
{
    public int Id { get; set; }
    public int GymId { get; set; }
    public Gym Gym { get; set; }
    public ICollection<Gym> Gyms { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public Address ShippingAddress { get; set; }
    public Address BillingAddress { get; set; }
    public string PhoneNumber { get; set; }
    public string EmailAddress { get; set; }
    [DataType(DataType.Date, ErrorMessage = "Invaild date.")]
    [Display(Name = "Date of birth")]
    public DateTime DateOfBirth { get; set; }

    public static implicit operator MembersViewModel(Member member)
    {
        return new MembersViewModel
        {
            BillingAddress = member.BillingAddress,
            FirstName = member.FirstName,
            Gyms = member.Gyms,
            Id = member.Id,
            LastName = member.LastName,
            MiddleName = member.MiddleName,
            ShippingAddress = member.ShippingAddress,
            DateOfBirth = member.DateOfBirth

        };

    }

    public static explicit operator Member(MembersViewModel member)
    {
        return new Member
        {
            BillingAddress = member.BillingAddress,
            FirstName = member.FirstName,
            Gyms = member.Gyms,
            Id = member.Id,
            LastName = member.LastName,
            MiddleName = member.MiddleName,
            ShippingAddress = member.ShippingAddress,
            DateOfBirth = member.DateOfBirth

        };

    }
}

它将允许我从日历中选择日期。是的,也将其保存在数据库中。但是当我想填充保存的日期时,文本框仍然为空。我不知道为什么,即使我保存了日期并传递给我的模型。有谁有想法吗?

4

1 回答 1

0

是的,我已经解决了这个问题,不确定是对还是错,但为我工作

 @Html.EditorFor(model => model.DateOfBirth)
 @Html.ValidationMessageFor(model => model.DateOfBirth)

 @Html.Hidden("hiddenFieldDateofBirth", Model.DateOfBirth.ToString("yyyy-MM-dd"))

 <script>
    $("#DateOfBirth").val($("#hiddenFieldDateofBirth").val());
</script>
于 2012-10-09T10:47:01.877 回答