When is it “acceptable” to use ViewBag/ViewData in ASP.NET MVC?

前端 未结 4 1371
时光说笑
时光说笑 2021-02-05 19:03

I realize that the best practice is to use strongly typed Views and pass in all needed data in a ViewModel, but I am curious if there are situations where it is actually conside

4条回答
  •  北荒
    北荒 (楼主)
    2021-02-05 19:23

    I typically will use a strongly typed view for displaying any content but will often set ViewBag.Member to the currently logged in member so that it can be used in the main Layout in addition to the specific view.

    I have an attribute called PopulateMemberContext which populates ViewBag.Member and I add that attribute to my base controller so that every view will always have the necessary data.

    "Right" or "Wrong" I don't know - but it works wonderfully.

提交回复
热议问题