c# mvc model vs viewbag

后端 未结 5 1239
滥情空心
滥情空心 2020-12-20 12:36

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 pas

相关标签:
5条回答
  • 2020-12-20 13:06

    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)
    
    0 讨论(0)
  • 2020-12-20 13:11

    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

    0 讨论(0)
  • 2020-12-20 13:12

    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.

    0 讨论(0)
  • 2020-12-20 13:15

    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.

    0 讨论(0)
  • 2020-12-20 13:20

    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.

    0 讨论(0)
提交回复
热议问题