如何通过MVC3中的ViewModel更新模型 [英] How to update Model via ViewModel in MVC3

查看:112
本文介绍了如何通过MVC3中的ViewModel更新模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我正在构建一个MVC3应用程序.我已经定义了数据模型,现在必须为特定操作创建ViewModels.

我正在使用自动映射器.这是一个示例情况:

Hi everyone

I''m building an MVC3 application. I have my data models defined, and now I have to create ViewModels for specific actions.

I''m using automapper. Here''s an example situation:

//Models

public class modelname{
  public int id {get;set;}
  [Required]
  public string name {get;set;}
  [Required]
  public string otherfield {get;set;}
  //many other properties
}

public class modelnameEditViewModel{
  public int id {get;set;}
  [Required]
  public string name {get;set;}
}


//global.asax
protected void Application_Start(){
   //....
   AutoMapper.Mapper.CreateMap<Models.Job, Models.JobEditViewModel>();
   AutoMapper.Mapper.CreateMap<Models.JobEditViewModel, Models.Job>();
   //...
}

//Controller

//get the model from db, returns a strongly-typed partialview for viewmodel;

public ActionResult Edit(int id)
{
  modelname model = db.modelname.find(id);
  modelnameEditViewModel viewmodel = Automapper.Mapper.Map<modelname, modelnameEditViewModel>(model);

 return PartialView("PartialEdit", viewmodel);
}



仅使用ViewModel的属性执行以下操作以更新模型是否正确?
将ViewModel放置在哪里的最佳实践"是什么?我可以将它们与模型保存在同一文件中吗?

预先感谢,
阿尔贝托




Is it correct to do the following to update the model with only the properties of ViewModel?
What is the "best practice" for where to place my ViewModels? can I keep them in the same file along with the Model?

Thanks in advance,
Alberto


Additional Tags are added.

推荐答案

那里有一个ViewModel类,用于将由类实例表示的多个数据封装到一个易于管理的对象中,您可以将该对象传递给您查看.

没有保存模型的好地方.如果项目很大并且有很多ViewModel(数据传输对象),则可以将它们保存在单独的程序集中.您也可以将它们保存在站点项目的单独文件夹中.
A ViewModel class is there to encapsulate multiple pieces of data represented by instances of classes into one easy to manage object that you can pass to your View.

There are no good place to keep your models in. You can keep them in separate assembly if the project is big and there are a lot of ViewModels (Data Transfer Objects). Also you can keep them in separate folder of the site project.


我不知道为什么,我的帖子已被编辑,问题中最重要的部分丢失了. br/>
通过具有主模型某些"属性的ViewModel更新主模型的问题..这样做是否正确?

I don''t know why, my post has been edited and the most important part of the question is missing..

The question about updating the main model via a ViewModel that has "some" of the properties of the main model.. is it correct to do it this way?

//http post from a strongly-typed partial view for ViewModel
[HttpPost]
public ActionResult Edit(modelnameViewModel viewmodel)
{  
   //get the original model from db
   modelname model = db.modelname.find(id);

   Automapper.Mapper.Map<modelnameeditviewmodel,>(viewmodel, model);//"maps" the values of the properties of the posted viewmodel into the "main" model

 UpdateModel(model);
 db.SaveChanges();

 return PartialView("FullDetails", model);
 
}



这确实有效,但是我不确定它是否形式正确"



This actually works, but I''m not shure about it being "formally correct" or not


这篇关于如何通过MVC3中的ViewModel更新模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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