Multiple Models in a Single View (C# MVC3)

前端 未结 6 1844
情深已故
情深已故 2020-12-03 03:49

I\'m using C# and MVC3.

I have a page, for example a Student list, that displays the list of students, which is database driven. At the same time my menu is database

6条回答
  •  醉酒成梦
    2020-12-03 04:23

    To handle multiple model in a single view, I personally use ViewBag.

    Please note that if you use ViewBag, all the help you get from the compiler is disabled and runtime errors/bugs will occur more likely than if the property has been on a "normal" object and typos would be catched by the compiler.

    That is the disadvantage of using dynamic objects, however, there are many other advantages. In your controller, you just need to pass the data/model into the ViewBag:

    public ActionResult Index() {
                ViewBag.TopMenu = TopMenu();
                ViewBag.Student = Student();
                return View();
            }
    

    Then in the view, call them out:

    @{
        ViewBag.Title = "Index_ViewBag";
    }
    
    

    Index View Bag

    @foreach (var menu in ViewBag.TopMenu) { }
    @menu.Name

提交回复
热议问题