在客戶端,我按照視圖需求自定義的方式顯示數據。將視圖模型重塑爲模型
當用戶保存,並將數據發送到我不想重塑上
客戶端的數據,以適應我的域名要求的服務器。
要重塑我的ViewModel我型我可以這樣做:
var periods = periodListViewModel.CellViewModels
.SelectMany(c => c.Rows)
.Select(
p =>
new Period { LessonNumber = p.LessonNumber });
此代碼必須在一個地方正確的責任付諸表決。
這些都是我的課:
public class SavePeriodListViewModel
{
public int SchoolyearId { get; set; }
public IEnumerable<CellViewModel> CellViewModels { get; set; }
}
public class CellViewModel
{
public CellViewModel()
{
Rows = new List<RowViewModel>();
}
public List<RowViewModel> Rows { get; set; }
}
public class RowViewModel
{
public string DayName { get; set; }
public string SchoolclassCode { get; set; }
public string Content { get; set; }
public int LessonNumber { get; set; }
}
public class LessonplannerResponse
{
public int SchoolyearId { get; set; }
public IEnumerable<Period> Periods { get; set; }
}
的所有數據,我需要爲我的模型週期是RowViewModel內。
我需要我的SavePeriodListViewModel重塑到LessonplannerResponse
當然AutMapper可以處理的RowViewModel以期映射,但我的問題是不是在這方面。
AutoMapper映射不會改變集合的形狀。
我應該在哪裏放置linq重塑代碼?或者更一般的問,我應該在asp.net mvc中把這些代碼放在哪裏?
- 客戶端?
- 控制器操作?
服務是沒有選擇的,因爲它不應該知道視圖模型。
Elist Automapper很好! – IamStalker 2013-03-02 12:34:46