c# - How to show data from two ViewModels in one View -
i fill 2 divs in view data 2 viewmodels, have problem.
my 2 viewmodels:
public class chatlogsnametimeviewmodel { public userprofile userprofile { get; set; } public string message { get; set; } public datetime time { get; set; } } public class userprofile { [key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int userid { get; set; } public string username { get; set; } public string email { get; set; } public bool isonline { get; set; } public virtual icollection<chatlogs> chatlogs { get; set; } }
which means want show data chatlogsnametimeviewmodel in 1 div in view , data userprofile in other div in view.
this viewmodel uses both viewmodels above:
public class chatlogsusersviewmodel { public ienumerable<chatlogsnametimeviewmodel> chatlogs { get; set; } public ienumerable<userprofile> users { get; set; } }
and index() action in controller:
var chatlogs = db.getchatlog().tolist(); var users = dba.getonlineusers().tolist(); var view = new chatlogsusersviewmodel(chatlogs, users); return view(view);
my problem can not access viewmodel attributes @ all.
when create foreach loop in view can access this:
means cannot access attributes @ print them in foreach.
i have in view:
@model ienumerable<chat.models.chatlogsusersviewmodel>
i assume not doing right in controller. have methods getchatlog()
, getonlineusers()
implemented in model, work alone no problem. don't know how make them work in 1 view.
you need update type of view.
you not passing view list of chat.models.chatlogsusersviewmodel
, have 1 , model has 2 lists.
so update to:
@model chat.models.chatlogsusersviewmodel
Comments
Post a Comment