我使用模型绑定运行了默认值的catch 22。 汤姆FitzMacken本教程有以下插入:ASP.NET Web表单模型绑定 - 插入时的默认值
public void addStudentForm_InsertItem()
{
var item = new Student();
TryUpdateModel(item);
if (ModelState.IsValid)
{
using (SchoolContext db = new SchoolContext())
{
db.Students.Add(item);
db.SaveChanges();
}
}
}
,但如果你有需要的领域,是不是在你的UI,说CreatedDate领域,TryUpdateModel将无法通过验证。
在调用TryUpdateModel = stuck之前,我看不到如何为其赋值。
item.CreatedDate = DateTime.Today设定值,但 TryUpdateModel(项目),然后刷新所有的值(即使不是在你的UI) –
@DerekMcLachlan调用'item.CreatedDate = DateTime.Today;''后TryUpdateModel(项目);' –
Thanks @Drew,但TryUpdateModel(item)验证并失败,因为缺少必需的日期,因此您无法在TryUpdateModel之后设置该值。 现在我坚持把它放在我的用户界面中。 这是我的第一个问题,所以我磕磕绊绊。我编辑了我的问题,你有任何ide如何获得静态DV值从生成的类和我的部分类? –