ASP.NET MVC - 部分更新从视图模型 [英] ASP.NET MVC - Partially updating model from view

查看:113
本文介绍了ASP.NET MVC - 部分更新从视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道人们是如何接近这一局面。这件事情,好像在我的MVC的使用一个薄弱点与奥姆斯(NHibernate的在这种情况下)...

I just wondered how people were approaching this situation. It's something that seems like a weak point in my usage of MVC with ORMs (NHibernate in this case)...

假设你有模型中的细粒度和复杂的实体。你可能有一个管理页面来管理此类型的对象。如果实体是复杂,这是不可能的,你会在一种形式来修改整个实体。你仍然需要当视图返回给相关特性传递给视图,并将变更到模型的属性。

Say you have a fine-grained and complicated entity in your model. You will likely have an admin page to manage objects of this type. If the entity is complicated, it is unlikely that you will be modifying the whole entity in one form. You still need to pass the relevant properties to the view, and incorporate changes to those properties in the model when the view returns them.

是什么人在这种情况下怎么办?

What does anyone do in this situation?


  • 创建,它是(或者包含)的实体属性的子集的图模型。通过这和从图。在控制器的编辑的操作方法,请从库中的对象,但走在视图模型的所有properies并将其应用到Model对象(model.a = viewmodel.a,modelb = viewmodel.b)。这似乎是显而易见的明智路线,但产生大量繁琐的管道code的。这也验证复杂一点。

  • Create a view model which is (or contains) a subset of the entities properties. Pass this to and from the view. In 'edit' action method in controller, get the object from repository, go though all the properies in the ViewModel and apply them to the Model object (model.a = viewmodel.a, modelb = viewmodel.b). This seems the obvious sensible route, but generates a lot of tedious plumbing code. Also this complicates validation a bit.

别的东西吗?

我在automapper抬头看了一眼 - 但是这似乎并不符合该法案究竟,也许我错了

I've looked briefly at automapper - but this doesn't seem to fit the bill exactly, maybe I'm wrong?

感谢。

推荐答案

这听起来像automapper完美的场景。创建包含字段或您的真实模型的子集视图模型类,你让AutoMapper照顾extraccting从域模型对象的值到您的视图模型对象。哪些问题是你用这种方法有?

This sounds like the perfect scenario for automapper. You create a view model class which contains a subset of the fields or your real model, and you let AutoMapper take care extraccting values from the domain model object into your view model object. What issues are you having with this approach?

考虑这个例子:

这是你的域模型和视图模型

public class Person
{
    public string FirstName
    { get; set; }

    public string LastName
    { get; set; }

    public string HomeNumber
    { get; set; }

    public string Address1
    { get; set; }

    public string Address2
    { get; set; }
}

public class PersonViewModel
{
    public string FirstName
    { get; set; }

    public string LastName
    { get; set; }

    public string HomeNumber
    { get; set; }
}

下面是你的贴图,你必须创建在两个方向上的映射从DM-> VM和VM-> DM。

Here is your mapping, you have to create a mapping in both directions from dm->vm and vm->dm.

这是我见过使用Automapper当是,如果你从映射对象A到B和B具有一个不具有的属性,它会被重置。所以,当我创建地图我直接就忽视这些缺少的属性。我不是一个专家Automapper,所以我可能会使用它错了。

From what I've seen when using Automapper is that if you map from object A to B and B has a property which A doesn't have, it will be reset. So when I create the map I direct it to ignore those missing properties. I'm not a Automapper expert so I may be using it wrong.

映射

Mapper.CreateMap<Person, PersonViewModel>();
// Automapper will reset values in dest which don't exist in source, so be sure to ignore them!
Mapper.CreateMap<PersonViewModel, Person>()
    .ForMember(dest => dest.HomeNumber, opt => opt.Ignore());

最后用法:

Person p = new Person()
{
    FirstName = "First",
    LastName = "Last",
    Address1 = "add 1",
    Address2 = "add 2"
};

PersonViewModel pvm = Mapper.Map<Person, PersonViewModel>(p);
// Map to a new person
Person p2 = Mapper.Map<PersonViewModel, Person>(pvm);

// Map to the existing person just to update it
Person p3 = new Person()
{
    HomeNumber = "numberHere"
};

// This will update p3
Mapper.Map<PersonViewModel, Person>(pvm, p3);

由于排斥的,这显然是不太理想,但比人工手动操作整个事情。

Because of the exclusion, this is obviously less than ideal, but much better than manually doing the whole thing.

这篇关于ASP.NET MVC - 部分更新从视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆