5

Is there a way to create a View() method that would return multiple objects, for example, I would like to call it something like this:

public class HomeController : Controller
{
  public ActionResult Index()
  {
    return View(CustomObject1 customObject1, CustomObject2 customObject2);
  }
}
2
  • 1
    What is your ultimate goal? Commented Dec 29, 2015 at 11:56
  • Well, I was doing some custom override for RazorViewEngine, and this came to my mind, so I wanted to see if this is possible, and to see how.. Commented Dec 29, 2015 at 13:24

1 Answer 1

7

Yes, it's possible, just create a view model:

public class MyViewModel
{
    public CustomObject1 CustomObject1 { get; set; }
    public CustomObject2 CustomObject2 { get; set; }
}

which you will pass to the view:

public ActionResult Index()
{
    var model = new MyViewModel();
    model.CustomObject1 = customObject1;
    model.CustomObject2 = customObject2;
    return View(model);
}

and finally make your view strongly typed to this view model:

@model MyViewModel

and access the corresponding properties when needed:

 <div>@Model.CustomObject1.FoorBar</div>
Sign up to request clarification or add additional context in comments.

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.