ASP.NET MVC视图模型。它们应该包含多少逻辑(如果有的话)?

5
我一直在研究mvc的视图模型,并正在寻找最佳方法。我已经阅读了很多不同的文章,但没有一篇文章能够清晰地说明“最佳方法”。例如,我可能有一个包含以下属性的客户模型:
  • 名字
  • 姓氏
  • 头衔
  • 位置
其中位置是数据库中位置表的外键。
我想编辑此客户,但仅限于名字、姓氏和位置。我不关心编辑中的头衔。所以,在我的视图中,我需要传递一个客户和一个选择列表。
从我阅读的内容来看,我有以下选项(可能还有更多)。
所以我的问题基本上是哪个是最好的?
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)

或者采用以上任意组合或其他方式。

欢迎各种意见。

2个回答

6

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

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

I'd have my ViewModel as this

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接