MVC 4 - Use a different model in partial view

前端 未结 4 431
走了就别回头了
走了就别回头了 2021-02-05 02:51

Please bear with my noobness, I\'m super new to the MVC pattern.

What I\'m trying to do

I am building a profile information page for re

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-05 03:38

    While I know this question has been asked longtime ago however some people might still face a similar problem. One easy solution I use to pass or have more than one view model on a page is to use a ViewBag to hold the second object and refer to it in the view. See example bellow.

    In your controller do this:

    Obj2 personalDets = new Obj2();
    DbContext ctx = new DbContext();
    var details = ctx.GetPersonalInformation;
    
    foreach(var item in details) {
        personalDets.Password = item.Password;
        personalDets .EmailAddress = item.EmailAddress; 
    }
    
    ViewBag.PersonalInformation = personalDets;
    

    Then in your view those properties become readily available for you

提交回复
热议问题