Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 年龄的价值没有';在我的项目中没有出现_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 3 年龄的价值没有';在我的项目中没有出现

Asp.net mvc 3 年龄的价值没有';在我的项目中没有出现,asp.net-mvc-3,Asp.net Mvc 3,我有这个模型: [DisplayName("Date of Birth:")] [Required(ErrorMessage = "Birth Date is Required.")] [DisplayFormat(NullDisplayText = "No Date of Birth is Selected.")] public DateTime BirthDate { get; set; } [DisplayName("Age:")] publi

我有这个模型:

 [DisplayName("Date of Birth:")]
    [Required(ErrorMessage = "Birth Date is Required.")]
    [DisplayFormat(NullDisplayText = "No Date of Birth is Selected.")]
    public DateTime BirthDate { get; set; }


    [DisplayName("Age:")]
    public int Age
    {
        get
        {
            DateTime now = DateTime.Today;
            int age = now.Year - BirthDate.Year;
            if (BirthDate > now.AddYears(-age)) age--;
            return age;
        }

    }
在我看来:

 <div class="editor">
 @Html.LabelFor(model => model.BirthDate)
 @Html.TextBoxFor(model => model.BirthDate, new { @class = "date" })
 @Html.ValidationMessageFor(model => model.BirthDate)
 </div>

 <div class="editor">
 @Html.LabelFor(model => model.Age)
 @Html.DisplayTextFor(model => model.Age)
 @Html.ValidationMessageFor(model => model.Age)
 </div>

@LabelFor(model=>model.BirthDate)
@Html.TextBoxFor(model=>model.BirthDate,新的{@class=“date”})
@Html.ValidationMessageFor(model=>model.BirthDate)
@LabelFor(model=>model.Age)
@DisplayTextFor(model=>model.Age)
@Html.ValidationMessageFor(model=>model.Age)
一旦填写了生日,年龄就不会显示计算的年龄。
如果我遗漏了什么,请告诉我?

我很确定您需要提供setter和getter,以便绑定正常工作


尝试为年龄添加setter,即使它不起任何作用。

我已经为年龄添加了setter,但仍然没有运气它没有出现在我的创建表单中。请帮帮我。我缺少什么?'public ActionResult Create(){return View();}///POST:/Employee/Create[HttpPost]public ActionResult Create(Employee-Employee){if(ModelState.IsValid){context.Employees.Add(Employee);context.SaveChanges();返回重定向到操作(“索引”);}返回视图(员工);}”