0

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

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

9 Comments

superb info can you please give me threads any link sorry to bother again as i'm new bie :(
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
what if SomeOtherProperty is also like GroupTypeNames i mean List i just need to take same as in viewmodel is it :)
Erm what? Sorry I don't understand what you are asking. You could have arbitrary complex properties in your view model.
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/…
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.