如何在视图模型中执行列表? +数据注释

发布于 2024-10-14 21:39:18 字数 2144 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2024-10-21 21:39:18

为什么要在视图模型中返回域对象列表?这不是视图模型应该有的样子。视图模型应该仅引用其他视图模型。所以你有一个很好的 UserViewModel 来代表一个用户。现在您需要在视图中处理多个用户,因此您可以传递 IEnumerable 或者如果您需要一些其他属性,您可以为此设计一个视图模型:

public class UserViewModel
{
    [Required]
    public string UserName = { get; set; }
    [Required]
    public string FirstName = { get; set; }
    [Required]
    public string LastName = { get; set; }
}

public class SomeViewModel
{
    public List<UserViewModel> Users { get; set; }
    public string SomeOtherProperty { get; set; }
}

现在您的控制器操作可能看起来像这样:

public ActionResult Foo()
{
    SomeModel model = _repository.GetModel();
    SomeViewModel viewModel = Mapper.Map<SomeModel, SomeViewModel>(model);
    return View(viewModel);
}

现在在您的视图中,您可以简单地使用此 Users 属性 (Html.DisplayFor(x => x.Users)) 的显示模板来显示列表其中。


更新:

看到您的更新后,以下是如何继续进行良好实践:

public ActionResult Foo()
{
    IEnumerable<Users> users = _repository.GetUsers();
    IEnumerable<UserViewModel> usersViewModel = Mapper
        .Map<IEnumerable<Users>, IEnumerable<UserViewModel>>(users);
    return View(usersViewModel);
}

我还使用了 AutoMap 示例项目 中的属性可以将您的代码简化为:

[AutoMap(typeof(IEnumerable<Users>), typeof(IEnumerable<UserViewModel>))]
public ActionResult Foo()
{
    IEnumerable<Users> users = _repository.GetUsers();
    return View(users);
}

此属性将在控制器操作之后和渲染视图之前自动运行,并使用 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 an IEnumerable<UserViewModel> or if you need some other properties you design a view model for this:

public class UserViewModel
{
    [Required]
    public string UserName = { get; set; }
    [Required]
    public string FirstName = { get; set; }
    [Required]
    public string LastName = { get; set; }
}

public class SomeViewModel
{
    public List<UserViewModel> Users { get; set; }
    public string SomeOtherProperty { get; set; }
}

and now your controller action might look like this:

public ActionResult Foo()
{
    SomeModel model = _repository.GetModel();
    SomeViewModel viewModel = Mapper.Map<SomeModel, SomeViewModel>(model);
    return View(viewModel);
}

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:

public ActionResult Foo()
{
    IEnumerable<Users> users = _repository.GetUsers();
    IEnumerable<UserViewModel> usersViewModel = Mapper
        .Map<IEnumerable<Users>, IEnumerable<UserViewModel>>(users);
    return View(usersViewModel);
}

I've also used an AutoMap attribute in a sample project which could simplify your code to this:

[AutoMap(typeof(IEnumerable<Users>), typeof(IEnumerable<UserViewModel>))]
public ActionResult Foo()
{
    IEnumerable<Users> users = _repository.GetUsers();
    return View(users);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文