asp.net-mvc – asp.net mvc viewmodels.它们应包含多少逻辑(如
发布时间:2020-12-16 07:15:15 所属栏目:asp.Net 来源:网络整理
导读:我一直在研究mvc的视图模型,我正在寻找最好的方法来实现它们.我读过很多不同的文章,但似乎没有一个明确的“最佳方式”.到目前为止,我可能有一个具有以下属性的Customer模型: 名字 姓氏 标题 位置 其中location是数据库中位置表的外键. 我希望能够编辑此客户
我一直在研究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(),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) 或者上述或其他方式的其他组合. 欢迎所有意见. 解决方法
以下是我的建议:有一个反映强类型视图字段的视图模型:
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,customer.Location); }; return View(viewModel); } [HttpPost] public ActionResult Index(SomeViewModel viewModel) { // TODO: Handle the form submission return View(viewModel); } 当然,如图所示,手动模型和视图模型之间的映射可能会变得非常麻烦,在这种情况下,我建议您查看AutoMapper. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何在ASP.NET中运行.py(Python)文件?
- asp.net – MVC3的远程模型验证操作中的参数名称
- asp.net-mvc – 尝试使用Moq模拟HtmlHelper时抛出MissingMe
- asp.net – SQL Server 2005:事务死锁
- asp.net-mvc – 如何在主机上运行ASP.Net MVC应用程序?
- asp.net-mvc – ASP.NET MVC3 IIS7.5:Cache-Control maxag
- ASP.NET NHibernate事务持续时间
- asp.net-mvc – 是否有针对ASP.NET MVC Production Apps的建
- asp.net – Request.IsLocal是安全还是可以欺骗?
- asp.net-mvc – Full-Stack Web应用程序框架的定义是什么
推荐文章
站长推荐
- asp.net-mvc – FormsAuthentication.SetAuthCoo
- asp.net – 实现自定义成员资格提供程序成员资格
- asp.net-mvc – 如何在MVC Filter属性中解析依赖
- ASP.NET 页面双向静态化
- System.Net.ServicePointManager.DefaultConnect
- asp.net-mvc – ASP.NET MVC 4 Datagrid
- asp.net核心身份提取并保存外部登录令牌并添加对
- asp.net – 在.NET中发生部分回发时,用户控件中的
- asp.net-mvc-3 – 从控制器发送电子邮件
- asp.net – 用于Webapp和桌面/移动应用程序的Ope
热点阅读