我有一个模型项目
public class EntryInputModel
{
...
[Required(ErrorMessage = "Description is required.", AllowEmptyStrings = false)]
public virtual string Description { get; set; }
}
和一个控制器动作
public ActionResult Add([Bind(Exclude = "Id")] EntryInputModel newEntry)
{
if (ModelState.IsValid)
{
var entry = Mapper.Map<EntryInputModel, Entry>(newEntry);
repository.Add(entry);
unitOfWork.SaveChanges();
return RedirectToAction("Details", new { id = entry.Id });
}
return RedirectToAction("Create");
}
EntryInputModel
当我在单元测试中创建一个,将Description
属性设置为null
并将其传递给操作方法时,我仍然得到ModelState.IsValid == true
,即使我已经调试并验证了newEntry.Description == null
.
为什么这不起作用?