从MVC中的viewModel填充模型的推荐方法是什么?

时间:2012-05-06 09:00:38

标签: asp.net-mvc asp.net-mvc-3

我有以下课程:

   public class City {
      public string key1 {get; set; }
      public string key2 { get; set; }
      public string col1 { get; set; }
      public string col2 { get; set; }
      public int newplusnew2 { get; set; }
   }

一个viewmodel:

   public class CityViewModel {
      public City city { get; set; }
      public int new1 { get; set; }
      public int new1 { get; set; }
   }

行动:

 public JsonResult JsonCreate(CityViewModel viewModel)
        {

 //  Validation checks go here

            var model = new City ();
            ???

   }

有人可以告诉我从viewmodel填充四个字符串列和int列的最佳方法吗?我只是想知道 如果有一些推荐的方法来做到这一点。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您从回发中获得了CityViewModel,并且您需要City ... ...但是远远看不出来,CityViewModel已经包含一个City对象,所以你可以这样做:

public JsonResult JsonCreate(CityViewModel viewModel)
{
    //  Validation checks go here
    var model = viewModel.city;
}