【问题标题】:asp.net mvc viewmodels. How much logic (if any) should they containasp.net mvc 视图模型。它们应该包含多少逻辑(如果有)
【发布时间】:2011-03-04 21:31:49
【问题描述】:

我一直在研究 mvc 的视图模型,并且正在寻找实现它们的最佳方法。我已经阅读了很多不同的文章,但似乎没有一个是明确的“最佳方式”。到目前为止,我可能有一个具有以下属性的 Customer 模型:

  • 名字
  • 姓氏
  • 标题
  • 位置

其中 location 是数据库中位置表的外键。

我希望能够编辑此客户,但只能编辑名字、姓氏和位置。我不介意编辑中的标题。所以在我看来,我需要传递一个客户和一个选定的列表。

现在根据我的阅读,我有以下选项(可能还有更多)。

所以我的问题基本上是哪个是最好的?

1)

ViewData["Location"] 添加一个选择列表并创建一个强类型的客户视图?

2)

创建一个视图模型,我在其中传递一个客户并选择列表(数据访问在控制器中完成):

public class ViewModelTest
{
    public Customer Customer { get; set; }
    public SelectList Locations { get; set; }

    public ViewModelTest(Customer customer, SelectList locations)
    {
        Customer = customer;
        Locations = locations;
    }
}

3)

创建一个视图模型,在其中传递客户和位置列表,并在视图模型中创建选择列表。

public class ViewModelTest
{
    public Customer Customer { get; set; }
    public SelectList Locations { get; set; }

    public ViewModelTest(Customer customer, List<Location> locations, string selectedLocation)
    {
        Customer = customer;
        Locations = new SelectList(locations, "LocationID", "LocationName", selectedLocation);
    }
}

4)

传递客户和存储库并在视图模型中进行数据访问。

public class ViewModelTest
{
    public Customer Customer { get; set; }
    public SelectList Locations { get; set; }

    public ViewModelTest(Customer customer, IRepository repository, string selectedLocation)
    {
        Customer = customer;
        Locations = new SelectList(repository.GetLocations(), "LocationID", "LocationName", selectedLocation);
    }
}

5)

仅使用我需要的属性创建视图模型:

public class ViewModelTest
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public SelectList Locations { get; set; }

    public ViewModelTest(Customer customer, SelectList locations)
    {
        FirstName = customer.FirstName;
        LastName = customer.LastName ;
        Locations = locations;
    }
}

6)

或以上或其他方式的其他组合。

欢迎所有意见。

【问题讨论】:

    标签: asp.net-mvc model-view-controller viewmodel


    【解决方案1】:

    以下是我的建议:有一个反映强类型视图字段的视图模型:

    public class SomeViewModel
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Location { get; set; }
        public IEnumerable<SelectListItem> PossibleLocations { get; set; }
    }
    

    并在您的控制器操作中填充此视图模型:

    public ActionResult Index()
    {
        var customer = Repository.GetCustomer();
        var locations = Repository.GetLocations();
        var viewModel = new SomeViewModel
        {
            FirstName = customer.FirstName,
            LastName = customer.LastName,
            Location = customer.Location,
            PossibleLocations = new SelectList(locations, "LocationID", "LocationName", customer.Location);
        };
        return View(viewModel);
    }
    
    [HttpPost]
    public ActionResult Index(SomeViewModel viewModel)
    {
        // TODO: Handle the form submission
        return View(viewModel);
    }
    

    当然,如我的示例所示手动执行模型和视图模型之间的映射可能会变得非常麻烦,在这种情况下,我建议您查看AutoMapper

    【讨论】:

      【解决方案2】:

      我的 ViewModel 是这样的

      public class SomeViewModel 
      { 
          public Customer Customer { get; set; } 
          public IEnumerable<Location> PossibleLocations { get; set; } 
      } 
      

      我的控制器是这样的:

      public ActionResult Index()   
      {     
          var viewModel = new SomeViewModel   
          {   
              Customer = Repository.GetCustomer(),
              PossibleLocations = Repository.GetLocations()
          };   
          return View(viewModel);   
      }
      

      然后您可以在视图中访问 Customer 对象中的所有内容,如下所示:

      Customer name - <%: Model.Customer.FirstName %> <%: Model.Customer.LastName %>
      Location - <%: Html.DropDownList("LocationID", new SelectList(Model.PossibleLocations as IEnumerable, "LocationID", "LocationName", Model.Location.LocationID))%>
      

      【讨论】:

        猜你喜欢
        • 2010-10-01
        • 2020-07-04
        • 1970-01-01
        • 2010-11-27
        • 2011-11-15
        • 2010-09-11
        • 1970-01-01
        • 1970-01-01
        • 2012-12-06
        相关资源
        最近更新 更多