Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have controller.cs code :

public class GroupsController : Controller
    {
        public ActionResult Index()
        {
            GroupTypeRepository groupTypeRepo = new GroupTypeRepository();

            var groupTypeNames = groupTypeRepo.GetAll().ToList();

    //like GroupType I have Group also so i want to pass both GroupType and Group object to my below return View("Groups", groupTypeNames);
        // how can i do this?

            return View("Groups", groupTypeNames);
        }

I tried it in doing in /Model Folder in Model.cs but its not good approach please guide how to achieve this? thanks

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

You can't return multiple models from a controller action. What you do instead is define a view model that will contain all the necessary information and then return this view model.

For example:

public class MyViewModel
{
    public List<string> GroupTypeNames { get; set; }
    public string SomeOtherProperty { get; set; }
}

and then:

public ActionResult Index()
{
     GroupTypeRepository groupTypeRepo = new GroupTypeRepository();

     var model = new MyViewModel();
     model.GroupTypeNames = groupTypeRepo.GetAll().ToList();
     model.SomeOtherProperty = "some other property value";

     return View("Groups", model);
}

Now of course your view should be typed to the view model:

@model MyViewModel

and if you wanted to access the some property:

@foreach (var item in Model.GroupTypeNames)
{
    <div>@item</div>
}

or:

@model.SomeOtherProperty
share|improve this answer
    
superb info can you please give me threads any link sorry to bother again as i'm new bie :( –  ashish Dec 29 '13 at 17:53
    
I don't understand what threads and links you are asking for. That's basic stuff. You should read the getting started tutorials at asp.net/mvc –  Darin Dimitrov Dec 29 '13 at 17:54
    
what if SomeOtherProperty is also like GroupTypeNames i mean List i just need to take same as in viewmodel is it :) –  ashish Dec 29 '13 at 17:55
1  
Erm what? Sorry I don't understand what you are asking. You could have arbitrary complex properties in your view model. –  Darin Dimitrov Dec 29 '13 at 17:55
    
hey related with previous question i used above code to return two model object but now getting error for previuos code for loop Operator '<' cannot be applied to operands of type 'int' and 'method group' question was stackoverflow.com/questions/20827155/… –  ashish Dec 29 '13 at 18:04
show 6 more comments

Using PartialResult You can divide your complex code into multiple Partials. For example here is the code.

 public class Type1
    {
        public struct Test
        {

        }
    }

    public class Type2
    {
    }

and this is my code that I used in partial

@using MVC5.Models

@model  List<Type1.Test>

in another partial you can call type2.

Now make a action in Controller like this

   public PartialViewResult Partial1()
        {
            ViewData.Model = new List<Type1.Test>();
            return PartialView();
        }

Now in my index.cshtml

@{
    ViewBag.Title = "Index";
}

@{
    Html.Action("Partial1","Home");   
}

I have called the partial1 in my index ActionResult. Using this implementation you can use model in every partialresult you have.

As darin say Make a call that hold all these sub-data and pass them from controller to Models.

I want to suggest you to pass multiple data on Views through ViewData or ViewBag (ViewBag is dynamic). See this post http://www.rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.