如何在视图模型中执行列表? +数据注释
我正在使用 asp.net mvc 3、数据注释和自动映射器。
一旦属性通过验证,我希望在视图模型中对属性进行所有注释,我使用自动映射器将其映射回我的域对象。
我有一个视图模型,其中包含我想要收集的属性,因为我想从它们生成一个表。我还想稍后使用它们作为向该表添加行的表单。
那我该怎么办?我如何获取这些属性并收集它们?
public class UserViewModel()
{
[Required()]
public string UserName = {get; set;}
[Required()]
public string FirstName = {get; set;}
[Required()]
public string LastName = {get; set;}
}
我想使用这些属性来生成我的表并用于表单。
我唯一能想到的就是这样做
public class AddUserViewModel()
{
[Required()]
public string UserName = {get; set;}
[Required()]
public string FirstName = {get; set;}
[Required()]
public string LastName = {get; set;}
}
public class UserViewModel()
{
public List<User> Users {get; set;}
public AddUserViewModel {get; set;}
public UserViewModel()
{
Users = new List<Users>();
}
}
所以基本上我将它作为一个单独的视图模型,包含在另一个包含用户列表(我的域模型)的视图模型中
这样我使用我的域模型来生成表和我的AddUserViewModel 用于添加用户。
似乎有点多余,所以我不确定是否有更好的方法。
编辑
我有类似的东西
var viewModel = new UserViewModel();
List<Users> users= UserService.GetAllUsers();
viewModel = Mapper.Map<Users, UserViewModel>(users);
return View("Index", viewModel);
我也尝试过
var viewModel = new UserViewModel();
List<Users> users= UserService.GetAllUsers();
viewModel.AddUserViewModel = Mapper.Map<Users, AddUserViewModel>(users);
return View("Index", viewModel);
编辑2
我有这个并且它可以编译,但我收到此错误
SomeViewModel viewModel = new SomeViewModel ();
List<User> users= userService.GetAllUsers();
viewModel.UserViewModel = Mapper.Map<List<User>, List<UserViewModel>>(users);
return View("Index", viewModel);
Trying to map Domain.User to ViewModels.UserViewModel.
Missing type map configuration or unsupported mapping.
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
I am using asp.net mvc 3, data annotations and auto mapper.
I want to have all my annotations on properties in my view model once the properties pass validation I use auto mapper to map it back to my domain object.
I have a viewmodel that has properties that I want to have a collection of since I want to generate a table from them. I also want to use them later to use as a form to add rows to this table.
So what do I do? How do I take these properties and make a collection of them?
public class UserViewModel()
{
[Required()]
public string UserName = {get; set;}
[Required()]
public string FirstName = {get; set;}
[Required()]
public string LastName = {get; set;}
}
I want to use these properties to generate my table and be used for a form.
The only thing I can think of is doing this
public class AddUserViewModel()
{
[Required()]
public string UserName = {get; set;}
[Required()]
public string FirstName = {get; set;}
[Required()]
public string LastName = {get; set;}
}
public class UserViewModel()
{
public List<User> Users {get; set;}
public AddUserViewModel {get; set;}
public UserViewModel()
{
Users = new List<Users>();
}
}
So basically I have it as a separate view model that is enclosed into another viewmodel that contains a list of Users(my domain model)
That way I use my domain model to generate the table and my AddUserViewModel for my adding of users.
Seems kinda redundant so I am not sure if there is a better way.
Edit
I have something like this
var viewModel = new UserViewModel();
List<Users> users= UserService.GetAllUsers();
viewModel = Mapper.Map<Users, UserViewModel>(users);
return View("Index", viewModel);
I also tried
var viewModel = new UserViewModel();
List<Users> users= UserService.GetAllUsers();
viewModel.AddUserViewModel = Mapper.Map<Users, AddUserViewModel>(users);
return View("Index", viewModel);
Edit 2
I have this and it compiles but I get this error
SomeViewModel viewModel = new SomeViewModel ();
List<User> users= userService.GetAllUsers();
viewModel.UserViewModel = Mapper.Map<List<User>, List<UserViewModel>>(users);
return View("Index", viewModel);
Trying to map Domain.User to ViewModels.UserViewModel.
Missing type map configuration or unsupported mapping.
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要在视图模型中返回域对象列表?这不是视图模型应该有的样子。视图模型应该仅引用其他视图模型。所以你有一个很好的
UserViewModel
来代表一个用户。现在您需要在视图中处理多个用户,因此您可以传递IEnumerable
或者如果您需要一些其他属性,您可以为此设计一个视图模型:现在您的控制器操作可能看起来像这样:
现在在您的视图中,您可以简单地使用此
Users
属性 (Html.DisplayFor(x => x.Users)
) 的显示模板来显示列表其中。更新:
看到您的更新后,以下是如何继续进行良好实践:
我还使用了 AutoMap 示例项目 中的属性可以将您的代码简化为:
此属性将在控制器操作之后和渲染视图之前自动运行,并使用 AutoMapper 将模型替换为相应的视图模型。
Why would you want to return a list of domain objects in your view model? That's not what view models are supposed to be. View models should reference only other view models. So you have a nice
UserViewModel
which represents a user. Now you need to work with multiple users in your view, so either you pass anIEnumerable<UserViewModel>
or if you need some other properties you design a view model for this:and now your controller action might look like this:
Now inside your view you could simply use a display template for this
Users
property (Html.DisplayFor(x => x.Users)
) to show a list of them.UPDATE:
After seeing your update here's how to proceed in terms of good practices:
I've also used an AutoMap attribute in a sample project which could simplify your code to this:
This attribute will automatically run after the controller action and before the view is rendered and would use AutoMapper to replace the model with the corresponding view model.