在 MVC 中查看模型对象的域模型对象
Domain Model Object to View Model Object in MVC
我想使用视图模型而不是域模型进行显示。我有这些视图模型 classes:
public class ArticleDescriptionViewModel
{
public string Title { get; set; }
public DateTime DateCreated { get; set; }
}
public class HomePage
{
public List<ArticleDescriptionViewModel> Articles { get; set; }
}
在我的领域模型中:
public class ArticleDescription
{
public string Title { get; set; }
public DateTime DateCreated { get; set; }
}
以及这种服务方式:
public List<ArticleDescription> GetArticlesDescription()
{
var articleDescription= from a in _ctx.Articles
select new ArticleDescription
{ Title = a.Title, DateCreated = a.DateCreated };
return articleDescription.ToList();
}
在控制器中,我想将视图模型 class 中的列表与域模型 class.
返回的列表相匹配
public ActionResult Index()
{
HomePage HomePageInstance = new HomePage();
HomePageInstance.Articles = _repo.GetArticlesDescription();
return View(HomePageInstance);
}
我有一个错误:
"Cannot implicitly convert type System.Collections.Generic.List (DBayonaCode.Domain.Services.Models.ArticleDescription)' to 'System.Collections.Generic.List(DBayonaCode.Models.ArticleDescriptionViewModel)'"
但这两个class是等价的吗?我做错了什么。感谢您的帮助?
ArticleDescription
和 ArticleDescriptionViewModel
是两种不同的类型,因此它们之间没有隐式转换。您需要将您的 域模型对象 映射到您的 视图模型对象 ,您可以手动或使用 这样的工具来完成AutoMapper.
您可以像这样编写扩展方法来进行映射:
public static class Mappings
{
public static ArticleDescriptionViewModel ConvertToView(this ArticleDescription article)
{
// Mapping Code
// return new ArticleDescriptionViewModel { ... }
}
public static List<ArticleDescriptionViewModel> ConvertToViews(this List<ArticleDescription> articles)
{
List<ArticleDescriptionViewModel> articleViews = new List<ArticleDescriptionViewModel>();
foreach (ArticleDescription article in articles)
{
articleViews.Add(article.ConvertToView())
}
return articleViews;
}
}
虽然 MVC 默认项目模板只提供一个模型文件夹,因此隐含地表达了模型是一回事的想法,但实际上 ASP.NET MVC 应用程序可能涉及三种类型的数据模型:
- Domain model objects will be passed from and to a middle tier services interfacing with databases.
- View Model objects are those that the Controller pass to the View.
- Input model objects are those that the default modelBinder or some custom modelBinder generates from the view, although in many cases the input models are the same view model objects.
希望对您有所帮助。
我想使用视图模型而不是域模型进行显示。我有这些视图模型 classes:
public class ArticleDescriptionViewModel
{
public string Title { get; set; }
public DateTime DateCreated { get; set; }
}
public class HomePage
{
public List<ArticleDescriptionViewModel> Articles { get; set; }
}
在我的领域模型中:
public class ArticleDescription
{
public string Title { get; set; }
public DateTime DateCreated { get; set; }
}
以及这种服务方式:
public List<ArticleDescription> GetArticlesDescription()
{
var articleDescription= from a in _ctx.Articles
select new ArticleDescription
{ Title = a.Title, DateCreated = a.DateCreated };
return articleDescription.ToList();
}
在控制器中,我想将视图模型 class 中的列表与域模型 class.
返回的列表相匹配public ActionResult Index()
{
HomePage HomePageInstance = new HomePage();
HomePageInstance.Articles = _repo.GetArticlesDescription();
return View(HomePageInstance);
}
我有一个错误:
"Cannot implicitly convert type System.Collections.Generic.List (DBayonaCode.Domain.Services.Models.ArticleDescription)' to 'System.Collections.Generic.List(DBayonaCode.Models.ArticleDescriptionViewModel)'"
但这两个class是等价的吗?我做错了什么。感谢您的帮助?
ArticleDescription
和 ArticleDescriptionViewModel
是两种不同的类型,因此它们之间没有隐式转换。您需要将您的 域模型对象 映射到您的 视图模型对象 ,您可以手动或使用 这样的工具来完成AutoMapper.
您可以像这样编写扩展方法来进行映射:
public static class Mappings
{
public static ArticleDescriptionViewModel ConvertToView(this ArticleDescription article)
{
// Mapping Code
// return new ArticleDescriptionViewModel { ... }
}
public static List<ArticleDescriptionViewModel> ConvertToViews(this List<ArticleDescription> articles)
{
List<ArticleDescriptionViewModel> articleViews = new List<ArticleDescriptionViewModel>();
foreach (ArticleDescription article in articles)
{
articleViews.Add(article.ConvertToView())
}
return articleViews;
}
}
虽然 MVC 默认项目模板只提供一个模型文件夹,因此隐含地表达了模型是一回事的想法,但实际上 ASP.NET MVC 应用程序可能涉及三种类型的数据模型:
- Domain model objects will be passed from and to a middle tier services interfacing with databases.
- View Model objects are those that the Controller pass to the View.
- Input model objects are those that the default modelBinder or some custom modelBinder generates from the view, although in many cases the input models are the same view model objects.
希望对您有所帮助。