时间:2019-03-17 标签:c#mvcmodelvsviewbag

发布于 2024-11-19 00:34:04 字数 764 浏览 2 评论 0原文

假设页面中有人员 A 的列表和人员 B 的列表。而这两个在L2S中是不同的类,代表两个不同的表。因此,您不能按如下方式传递单个模型:

...
@model PeopleA
...
@foreach(var peopleA in Model.People) ...

@foreach(var peopleB in //what?)

因此,我想我有三个选择可供遵循。

  • 第一个是将页面划分为部分视图,以便我可以通过 RenderAction 帮助器传递模型。因为只有当这个选项似乎对我不有吸引力时,我才会使用这些部分视图。
  • 第二个选择是使用 ViewBags,但我不想这样做,因为我更喜欢强类型模型。
  • 最后一个,我即将使用但想在这样做之前询问一下,是创建一个模型,如下所示:

ModelMyPage.cs

public List<PeopleA> peopleA { get; set; }
public List<PeopleB> peopleB { get; set; }

MyController.cs

... 
ModelMyPage m = new ModelMyPage();
m.peopleA = // query
m.peopleB = // another query
return(m);

您明白了。这是完成我的任务的有效方法还是有更好的 C# 方法来完成我想要的事情?

Suppose you have a list of People A and a list of People B in a page. And these two are seperate classes in L2S, representing two different tables. Therefore, you cannot pass a single model as follows:

...
@model PeopleA
...
@foreach(var peopleA in Model.People) ...

@foreach(var peopleB in //what?)

Accordingly, I guess, I have three options to follow.

  • The first one is to devide the page into partial views so that I can pass a model through RenderAction helper. Since I will use these partial views only once this option does not seem attracting to me.
  • The second option would be to use ViewBags which I don't want to since I prefer strongly typed models.
  • The last one, finally, which I was about to use but wanted to ask before doing so, is to create a model as the following:

ModelMyPage.cs

public List<PeopleA> peopleA { get; set; }
public List<PeopleB> peopleB { get; set; }

MyController.cs

... 
ModelMyPage m = new ModelMyPage();
m.peopleA = // query
m.peopleB = // another query
return(m);

And you got the idea. Is this the valid way to accomplish my task or is there a better c# way to do what I want?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无人问我粥可暖 2024-11-26 00:34:04

创建一个特定于页面的 ViewModel,因为你的选项 3 就是我要做的方式。

我相信这也是推荐的方法。

Creating a ViewModel specific to the page, as your option 3 is the way I would do it.

I believe this is also the recommended approach.

﹏雨一样淡蓝的深情 2024-11-26 00:34:04

不,没有更好的主意了。在 asp.net MVC 中,M 代表 ViewModel,而不是 Business、Domain 模型。建议为您的视图创建 ViewModel,不建议使用业务模型。您应该设计 ViewModel 以满足控制器与域交互以及从控制器到视图交互的需求

No, there is not any better idea. In asp.net MVC, M stands for ViewModels, not the Business, Domain models. It is recommended to create ViewModels for your views and it's not reccomended to use Business Models. You should design your ViewModels to fit the need of controller interactions with Domain, and from controller to view interactions

弃爱 2024-11-26 00:34:04

您的第一个和第三个选项似乎都可以。

ad 1)“只使用一次”并不是一个很好的反对理由。使用部分视图来组织视图。

ad 2) 使用 Viewbag 添加小项目,例如查找列表。

ad 3) ViewModel 在 MVC 中(变得)很常见。这可能是最好的方法。

Your first and third options seem both OK.

ad 1) "only using them once" is not a good argument-against. Use Partial views to organize views.

ad 2) Use the Viewbag to add small items like a lookup list.

ad 3) ViewModels are (becoming) common in MVC. This is probably the best approach.

微暖i 2024-11-26 00:34:04

我会用第三种方法。另外,如果你要为两个数组中的每个人渲染相同的 html,我会在 foreach 之前将它们连接起来:

var person in Model.PeopleA.Concat(Model.PeopleB)

I would do it the third way. Additionally, if you are going to render identical html for each person in both arrays, I would concat them before foreach:

var person in Model.PeopleA.Concat(Model.PeopleB)
此生挚爱伱 2024-11-26 00:34:04

我通常为页面创建一个模型,并对其进行命名,例如 AccountDetailsPageModel。然后其他模型可以针对复杂页面使用此属性。

I usually create a Model for the page, and name it as such, eg AccountDetailsPageModel. Then other models can be properies of this for complex pages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文