我有一個MVC2應用程序使用MVVM模式。我正在嘗試使用數據註釋來驗證表單輸入。ViewModel MVC2 DataAnnotations - 不明白與MVVM模式使用它
在我ThingsController我有兩種方法:
[HttpGet]
public ActionResult Index()
{
return View();
}
public ActionResult Details(ThingsViewModel tvm)
{
if (!ModelState.IsValid) return View(tvm);
try
{
Query q = new Query(tvm.Query);
ThingRepository repository = new ThingRepository(q);
tvm.Things = repository.All();
return View(tvm);
}
catch (Exception)
{
return View();
}
}
我Details.aspx看法是強類型的ThingsViewModel:
<%@ Page Title=""
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<Config.Web.Models.ThingsViewModel>" %>
視圖模型是由返回事的IList中的類對象和查詢字符串(它在表單上提交)並具有所需的數據註釋:
public class ThingsViewModel
{
public IList<Thing> Things{ get; set; }
[Required(ErrorMessage="You must enter a query")]
public string Query { get; set; }
}
當我運行它,然後點擊提交表單按鈕不輸入值我出現以下錯誤YSOD:
The model item passed into the dictionary is of type
'Config.Web.Models.ThingsViewModel', but this dictionary
requires a model item of type
System.Collections.Generic.IEnumerable`1[Config.Domain.Entities.Thing]'.
我怎樣才能得到數據註釋與一個ViewModel工作?我看不到我錯過了什麼,或者我錯了什麼地方 - 在我開始驗證之前,虛擬機工作得很好。