1

I have a controller that takes in an instance of getbldgaddr_Result. I am trying to create a view that uses partials to put two separate viewmodels on the page. Below is the "Parent View"

@using DirectoryMVC.Models

@model  Tuple<getbldgaddr_Result,getadministrators_Result>


@{
    ViewBag.Title = "Central Office";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Central Office</h2>
<div class="row">
    @*Display the getAddress view*@
    @Html.Partial("_BuildingAddress", Model.Item1)

</div>

So above I am using Item1 from the model that is a getbldgaddr_Result. I have tried casting it to getbldgaddr_Result as well, but I get the below error.

The model item passed into the dictionary is of type 'System.Tuple`2[DirectoryMVC.Models.getbldgaddr_Result,DirectoryMVC.Models.getadministrators_Result]', but this dictionary requires a model item of type 'DirectoryMVC.Models.getbldgaddr_Result'.

The actual controller is...

public class BuildingAddressController : Controller
    {
        private DirectoryEntities de;

        public BuildingAddressController()
        {
            de = new DirectoryEntities();
        }
        // GET: BldgAddr
        public ActionResult BldgAddr(getbldgaddr_Result bldgres)
        {
            //getbldgaddr_Result bldgResult  = de.getbldgaddr(district, bldg_no, null).FirstOrDefault();
            return View(bldgres);
        }
    }

My guess is, I can have it take a tuple object as the parameter, but this seems counter intuitive. the Building Address, does not need anything in regards to administration. Any thoughts?

christopher clark
  • 2,026
  • 5
  • 28
  • 47

2 Answers2

2

Why send a Tuple back to the view? Just create a parent viewmodel having two child viewmodels—i.e. one for each item of the tuple—and return that from the controller instead.

Assuming a Tuple<Class1, Class2>, you could structure your viewmodel like this:

public class ParentViewModel
{
    public Class1 ChildViewModel1 { get; set; }
    public Class2 ChildViewModel2 { get; set; }
}

On the controller side, you'd simply do:

var parentViewModel = new ParentViewModel 
                      { 
                          ChildViewModel1 = Tuple.Item1, 
                          ChildViewModel2 = Tuple.Item2 
                      };
return View(parentViewModel);

Your view would then be:

@model ParentViewModel

// ...
@Html.Partial("_Partial1", Model.ChildViewModel1)
@Html.Partial("_Partial2", Model.ChildViewModel2)
trashr0x
  • 6,457
  • 2
  • 29
  • 39
0

Make sure that the Model.Item1 is not null. When the member/property you're passing in is null, MVC reverts back to the parent type.

Tacud
  • 609
  • 5
  • 16