我是 MVC 的新手。我面临使用视图模型将出生日期值从视图获取到控制器的问题。我正在使用视图模型来获取表单值并获取所有其他文本框值。但我总是得到日期的空值。
我的视图模型是这样的。
  public class Student
  {
       [Required(ErrorMessage = "Date of birth is required")]
       [DataType(DataType.Date), DisplayFormat(ApplyFormatInEditMode = true,DataFormatString = "{0:dd/MM/yy}",ConvertEmptyStringToNull=false)]
       public DateTime? DateOfBirth { get; set; }
  }
我在表单发布后调用的控制器是:
 [HttpPost]
    public ActionResult StudentPersonalReg(Student stud)
    {
        DateTime? dateofbirth = stud.DateOfBirth;     
        return RedirectToAction("Registration");
    }
我的看法是
  @model eEducation.Models.UserModel.Student
   @Html.TextBoxFor(x => x.DateOfBirth)
   @Html.ValidationMessageFor(x => x.DateOfBirth)
问题是我总是在控制器中得到空值的日期。当我从控制器调用视图时,我还尝试将 DateTime.Now 设置为视图模型。
我没有使用强类型视图。
请在这方面帮助我。
我正在通过控制器方法传递我的模型以查看
    public ViewResult Registration()
    {
        var db = new eEducationEntities();
        List<CountryMaster> queryCountry = db.CountryMasters.ToList();         
        List<StateMaster> queryState = db.StateMasters.ToList();
        Student stds = new Student();
        stds.Countries = queryCountry;
        stds.States = queryState;
        stds.DateOfBirth = DateTime.Now;
        return View("~/Views/UserSection/StudentRegistration.cshtml", stds);
    }