1
的基本想法是,你有一些類具有引用類型屬性,像這樣創建在asp.net mvc的一個創建/編輯視圖/帶的SelectList行動
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Country HomeCountry { get; set; }
}
public class Country
{
public int Id { get; set; }
public string Name { get; set; }
}
現在,如果你需要創建一個視圖來編輯個人數據你沒有任何名稱或編號的問題,但你有國家;
目前我有兩個基本的解決方案
NR1:的操作方法,我把 ViewData["Countries"] = countryService.GetAll();
並在視圖像這樣的東西 SelectList(ViewData["Countries"], Country.Id
使用它,我要去有[後操作]創建(國家/地區)
nr2:創建PersonDto或PersonViewModel(同一件事我猜)
這裏我就不得不創建(CountryVieModel countryViewModel),沒有的ViewData
public class PersonViewModel
{
Person Person { get; set; }
public SelectList Countries { get; private set; }
public PersonViewModel(Person person)
{
Person = person;
var countryService = container.Resolve<CountryService>();
Countries = new SelectList(countryService.GetAll(),Person.Country);
}
}
的用法,但我覺得應該有一些辦法比這兩個
任何人都更加了解做到這一點的最佳方式?
其實我以爲應該有比這兩個更好的東西;在nr2中,我打電話給ViewModel內的數據庫 – Omu 2009-11-05 14:21:47
我更喜歡這樣做,但大多數人不這樣做。這是另一個可能會幫助你的討論http://stackoverflow.com/questions/1593406/who-has-the-responsibilty-of-loading-data/1594300#1594300 – 2009-11-05 15:08:10