asp.net mvc 视图模型。它们应该包含多少逻辑(如果有)
我一直在研究 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)
或上述或其他方式的其他组合。
欢迎所有意见。
I've been looking into view models for mvc and I'm looking for the best way to do them. I've read loads of different articles but none seem to be clear as the "best way." So far example I might have a Customer model with the following properties:
- First Name
- Last Name
- Title
- Location
Where location is a foreign key to a location table in the database.
I want to be able to edit this customer but only the first name, last name and location. I'm not bothered about the title in the edit. So in my view I will need to pass a customer and a selected list.
Now from what I've read I have the following options (there's probably many more).
So my question is basically which is the best one?
1)
Add a select list to the ViewData["Location"]
and just create a strongly typed view of customer?
2)
Create a view model where I pass a customer and select list (the data access is done in the controller):
public class ViewModelTest
{
public Customer Customer { get; set; }
public SelectList Locations { get; set; }
public ViewModelTest(Customer customer, SelectList locations)
{
Customer = customer;
Locations = locations;
}
}
3)
Create a view model where I pass a customer and list of locations and create the select list in the view model.
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)
Pass a customer and repository and do the data access in the view model.
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)
Create the view model with just the properties I need:
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)
Or some other combination of the above or another way.
All opinions welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的建议:拥有一个反映强类型视图字段的视图模型:
并在控制器操作中填充此视图模型:
当然,如我的示例所示,手动进行模型和视图模型之间的映射可能会变得相当麻烦在这种情况下,我建议您查看 AutoMapper。
Here's what I may suggest: have a view model which reflects the fields of strongly typed view:
And in your controller action populate this view model:
Of course doing the mapping between the model and the view model manually as shown my example could become quite cumbersome and in this case I would recommend you looking at AutoMapper.
我将我的 ViewModel 作为
我的控制器,如下所示:
然后您可以在视图中访问 Customer 对象中的所有内容,如下所示:
I'd have my ViewModel as this
My controller like this:
and then you can access everything in your Customer object in the view like this: