Home > Net >  EF multi-table query results into the ViewModel
EF multi-table query results into the ViewModel

Time:05-27

I in the project through the ef two table query to obtain a var data collection
 

Public dynamic GetAllPalte ()
{
Using (CSDC_DBEntities context=new CSDC_DBEntities ())
{
Var paltes=the from p in the context. T_palte
The join u in the context. T_user on p.u _id equals u.u _id
Select new
{
P_id=p.p _id,
P_name=p.p _name,
P_logo=p.p _logo,
P_introduce=p.p _introduce,
P_discussnums=p.p _discussnums,
U_id=u.u _id,
U_name=u.u _username

};
Return paltes. ToList ();

}
}

I think it is in the controller is converted to the ViewModel is bound to the View of
What should I do?

CodePudding user response:

I have done such a try
The ViewModel code
 
Public class BBSModel
{
Public int p_id {get; set; }
Public string p_name {get; set; }
Public string p_logo {get; set; }
Public string p_introduce {get; set; }
Public int u_id {get; set; }
Public string u_username {get; set; }
}

The controller code
 
Public ActionResult (BBS)
{
BBSModel BBSModel=new BBSModel ();
BbsModel=discussManager. GetALLPalte ();


Return the View (bbsModel);
}

But in my View when using the

CodePudding user response:

The View in the View Model is considered to be the current View of entity object, the object does not support iteration,

CodePudding user response:

@ model in the view at the top of the statement, if return from controller is entity, is declared as @ model XXX. XXX. BBSModel, if it is other types, the statement for the corresponding type can, for example:
@ model List , this Model is List , can be normal iteration,

CodePudding user response:

reference exception92 reply: 3/f
the top statement @ model in the view, if return from controller is entity, is declared as @ model XXX. XXX. BBSModel, if it is other types, the statement for the corresponding type can, for example:
@ model List , this Model is List , can be normal iteration,

Thank you
Then I logic layer GetALLPalte method return types should be defined as LIst<?> What type? , I am so ViewMoel definition is correct?

CodePudding user response:

reference 4 floor branchlets _3 response:
Quote: refer to the third floor exception92 response:
the top statement @ model in the view, if return from controller is entity, is declared as @ model XXX. XXX. BBSModel, if it is other types, the statement for the corresponding type can, for example:
@ model List , this Model is List , can be normal iteration,

Thank you
Then I logic layer GetALLPalte method return types should be defined as LIst<?> What type? , I am so ViewMoel definition is correct?

Use weak type ViewBag or select new direct assignment in the View of a strongly typed into List CodePudding user response:

reference 5 floor exception92 reply:
Quote: refer to the 4th floor branchlets _3 reply:
Quote: refer to the third floor exception92 response:
the top statement @ model in the view, if return from controller is entity, is declared as @ model XXX. XXX. BBSModel, if it is other types, the statement for the corresponding type can, for example:
@ model List , this Model is List , can be normal iteration,

Thank you
Then I logic layer GetALLPalte method return types should be defined as LIst<?> What type? , I am so ViewMoel definition is correct?

Use weak type ViewBag or select new direct assignment in the View of a strongly typed into List
whether is me the above example for example, trouble, I still have a doubt, the strong type is the viewmodel?)

CodePudding user response:

reference 5 floor exception92 reply:
Quote: refer to the 4th floor branchlets _3 reply:
Quote: refer to the third floor exception92 response:
the top statement @ model in the view, if return from controller is entity, is declared as @ model XXX. XXX. BBSModel, if it is other types, the statement for the corresponding type can, for example:
@ model List , this Model is List , can be normal iteration,

Thank you
Then I logic layer GetALLPalte method return types should be defined as LIst<?> What type? , I am so ViewMoel definition is correct?

Use weak type ViewBag or select new direct assignment in the View of a strongly typed into List

I try to use the Select new a var. Tolist (), the method return type in public List GetALLPalte (), "List" An error in the
  • Related