1
我在EF模型中驗證問題,我似乎無法弄清楚。不顯眼的Javascript驗證部分按預期工作。EF複雜類型和驗證
考慮以下車型(該RequiredIf
屬性爲this library):
public class Conversation
{
public int Id { get; set; }
public User User { get; set; }
public String Handler { get; set; }
}
[ComplexType]
public class User
{
public bool Anonymous { get; set; }
[RequiredIf("Anonymous", false)]
[Display(Name = "Full name")]
public String Name { get; set; }
}
我的編輯視圖僅顯示字段User
,這是我的控制器。
[HttpPost()]
public ActionResult Create(Conversation conversation)
{
if (ModelState.IsValid)
{
_db.Conversations.Add(conversation);
_db.SaveChanges(); // fails on this line
}
return RedirectToAction("Index");
}
這將導致以下錯誤:
DbUnexpectedValidationException: An unexpected exception was thrown during validation of 'Full name' when invoking Mvc.ValidationToolkit.RequiredIfAttribute.IsValid. See the inner exception for details.
而且內部異常:
Member 'Conversation.Anonymous' not found.
爲什麼突然驗證尋找Conversation.Anonymous
,而不是Conversations.Client.Anonymous
?