Pas*_*cal 5 asp.net asp.net-mvc mvvm viewmodel
我使用Automapper或手动映射,它不起作用.
ReleaseViewModel的所有数据必须首先在Release中,因为它随数据访问层填充.90%的模特都是这样的.为什么重复一切的开销?
KISS原理和过度工程怎么样?
当然,每个工具都适合其适当的任务,但我经常读到,在asp.net mvc中不使用ViewModel是NO-GO.
在哪里划线?当我们从模型中区分到50%,75%或99%时,我应该使用ViewModels吗?
我有一个模型发布:
public class Release
{
public int Id { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public DateTime CreatedAt { get; set; }
public int FailedTestsCount { get; set; }
public int SucceededTestsCount { get; set; }
public int SumTestsCount
{
get
{
return SucceededTestsCount + FailedTestsCount;
}
}
public int SumTestingTime { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
一个viewmodel ReleaseViewModel:
public class ReleaseViewModel
{
[HiddenInput(DisplayValue = false)]
public int Id { get; set; }
[Required(ErrorMessage = "Name must not be empty.")]
[StringLength(30, ErrorMessage = "Enter max. 30 chars for a name.")]
[Remote("ReleaseExists", "Release", ErrorMessage = "This name already exists.")]
public string Name { get; set; }
public string Author { get; set; }
public DateTime CreatedAt { get; set; }
public int FailedTestsCount { get; set; }
public int SucceededTestsCount { get; set; }
public int SumTestsCount
{
get
{
return SucceededTestsCount + FailedTestsCount;
}
}
public int SumTestingTime { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
ViewModel是VIEW的东西.大部分时间它与您的实体模型类似.但不总是.
看看你的例子.在您的中ViewModel,您具有Remote属性和一些验证属性.因此,您可以添加此远程名称检查,以便为用户提供更好的用户体验.它特定于View.
您需要Viewmodel的另一个场景是用于涉及多个模型的屏幕.例如:您有一个User实体和一个Project实体,并且您想要提供一个可以将项目添加到用户的屏幕.所以在这种情况下,您可以创建一个viewmodel来处理它
public class ProjectToUserVM
{
public int UserId { set;get;}
public string UserName { set;get;} // i want to display only name of user!
public int ProjectID { set;get;}
public IEnumerable<SelectListItem> Projects { set;get}
}
Run Code Online (Sandbox Code Playgroud)
不要将ViewModel用于所有模型实体.当您的VIEW确实需要时创建它.我在一些视图中直接使用我的Model实体对象,而不创建一个viewmodel,因为它们完全相同.例如:国家/州/市(查找表数据.无添加/编辑)
| 归档时间: |
|
| 查看次数: |
560 次 |
| 最近记录: |