关于模型和视图的最佳实践是什么?
我一直在开发一个应用程序,该应用程序使用基于单个 USP/dbml 的模型,并使用 ViewBags 填充数据输入和编辑视图的查找列表。在我开始实现业务逻辑之前,这一直没问题。下拉列表因为是使用 ViewBag.llkup 完成的,所以在脚本函数中不可用,除非显式传递给函数。我可以创建一个包含多个 USP 的单一模型。我想知道关于这个问题的最佳实践是什么。 谢谢 布鲁斯 我在控制器中有以下代码:
using (var dc = new usp_TM_Select_ShortNameDataContext())
{
String[] s = this.User.Identity.Name.Split('\\');
string[] s2 = s[1].Split('.');
string a = s2[0] + '.' + s2[1];
ViewBag.Message = a.ToString();
ViewBag.DetailsList = new SelectList((System.Collections.IEnumerable)dc.usp_TM_Select_ShortName().ToList(), "short_title", "short_title");
var poc = new usp_ARD_Select_POCDataContext();
return View(poc.usp_ARD_Select_POC().Single());
}
两者都是强类型视图,但是如果它们位于单个模型中,我如何将它们返回到视图以便可以使用(访问)它们? 我有更复杂的示例,其中我通过多次查找返回了一条记录。我目前正在使用 ViewBag 执行此操作,并希望使用 ViewModels。 谢谢 布鲁斯
I have been developing an application that uses Models that are based on a single USP/dbml and using ViewBags to populate the look up lists for data entry and edit views. This has been ok until I start to implement business logic. The Dropdownlists because they are done with ViewBag.llkup aren't available in the Script functions unless explictly passed to the function. I could create a single model that had multiple USP in it. I was wondering what is considered best practice concerning this issue.
Thanks
Bruce
I have the following code in a controller:
using (var dc = new usp_TM_Select_ShortNameDataContext())
{
String[] s = this.User.Identity.Name.Split('\\');
string[] s2 = s[1].Split('.');
string a = s2[0] + '.' + s2[1];
ViewBag.Message = a.ToString();
ViewBag.DetailsList = new SelectList((System.Collections.IEnumerable)dc.usp_TM_Select_ShortName().ToList(), "short_title", "short_title");
var poc = new usp_ARD_Select_POCDataContext();
return View(poc.usp_ARD_Select_POC().Single());
}
Both are strongly typed views but how do I return both if they are in a single model to the view so that they can be used (accessed)?
I have more complex examples where I have a Single record returned with multiple lookups. I am currently doing this with ViewBag and would like to use ViewModels.
Thanks
Bruce
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将视图强类型化为 ViewModel 将是我的选择。
您可以在此处查看示例:
http://www. bidn.com/blogs/mbrown/development/2139/mvc-3-view-models
何时使用视图模型、部分、模板并使用 MVC 3 处理子绑定
Having the view strongly typed to a ViewModel would be my choice.
You can see examples here:
http://www.bidn.com/blogs/mbrown/development/2139/mvc-3-view-models
When do I use View Models, Partials, Templates and handle child bindings with MVC 3
99% 的情况下,最佳实践是基于模型(或
ViewModel
,如果适用的话)创建强类型视图。这样您就可以针对特定领域的模型定制视图。The best practice 99% of the time is to create a strongly type View based off of your Models (or
ViewModel
, if it so applies). That way you can tailor your Views for the domain-specific Model(s).