Click here to Skip to main content
15,037,420 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my controller code, I need to return two objects to the Details view (details.cshtml)

Below is the Controller code:
C#
FinishProductCompleteForm FinishProductCompleteFormDetails = FinishProductCompleteFormDAL.GetFPCompletelDetailsByID(objFinishProductCompleteForm);

FinishProductCompleteForm objRawMatDetails = FinishProductCompleteFormDAL.GetDashboardInfo(FinishProductCompleteFormDetails);

return View(FinishProductCompleteFormDetails);


Currently I just can return FinishProductCompleteFormDetails only, but I need to return both FinishProductCompleteFormDetails and objRawMatDetails.

thanks
Posted
Updated 21-Jun-15 17:30pm
v2
Comments
Mathew Soji 22-Jun-15 1:29am
   
Why not create a View model that returns two or more objects and return this View Model back to the controller.Refer below solution .

http://stackoverflow.com/questions/20828237/how-to-return-multiple-model-object-from-one-index-controller-method-in-mvc
Arkadeep De 22-Jun-15 1:55am
   
You can use two different ViewBag to do so...but it is not so good practice...better to go with mathew's solutions.

1 solution

Hello ,

You can create a custom model representing the data needed for your view.

<pre lang="c#">
public class UserView
{
public User User{get;set;}
public List Messages{get;set;}
}

And then,

return View(new UserView(){ User = user, Messages = message});

In the view:

Model.User;
Model.Messages;

The ViewBag is useful because it is dynamically typed, so you can reference members in it directly without casting. You do, however, then lose static type checking at compile time.

ViewData can be useful if you have a one-off on your view data types and know the type and will be doing a cast in the view anyway. Some people like to keep the actual typed view pure in a sense that it represents the primary model only, others like to take advantage of the type checking at compile time and therefore make custom models needed for the view.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900