价值时代并没有出现在我的项目
问题描述:
我有这样的模式:价值时代并没有出现在我的项目
[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>
而且一旦生日充满时代不显示计算的年龄。 让我知道如果我错过了什么?
答
我敢肯定,你需要提供一个setter和getter绑定才能正常工作。
尝试为Age添加setter,即使它没有做任何事情。
我已经为Age添加了一个setter,但仍然没有运气它不会出现在我的创建表单中。请帮帮我。我错过了什么? – keizune 2012-02-08 08:50:55
您是否也可以显示控制器操作的代码。 – 2012-02-08 16:35:04
'public ActionResult Create() { return View(); } // // POST:/雇员/创建 [HttpPost] 公共的ActionResult创建(雇员雇员) { 如果(ModelState.IsValid) { context.Employees.Add(雇员); context.SaveChanges(); return RedirectToAction(“Index”); } return View(employee); }' – keizune 2012-02-09 09:13:16