How to render 2 typed view from different controllers

Asked

Viewed 46 times

3

How do I render 2 view typed of controllers different?

Controller:

public ActionResult Index()
    {
        Cliente cli = new Cliente();
        cli.Nome = "Al Unser";

        return View(cli);
    }

Index:

 @model MvcRazor.Entidades.Cliente

    @{
        ViewBag.Title = @Model.Nome;
    }

    <h2 id="teste">@Model.Nome</h2>

    @Html.Partial("~/Views/Shared/_Pedidos.cshtml")

    <script src="~/Scripts/Home.js"></script>

Partial View:

@model MvcRazor.Entidades.Pedido

<h1>@Model</h1>

1 answer

1

The right way is to bring everything into the same Action:

public ActionResult Index()
{
    Cliente cli = new Cliente();
    cli.Nome = "Al Unser";
    cli.Pedidos = new List<Pedido> 
    {
        // Lance aqui seus pedidos
    }

    return View(cli);
}

View:

@model MvcRazor.Entidades.Cliente

@{
    ViewBag.Title = @Model.Nome;
}

<h2 id="teste">@Model.Nome</h2>

@foreach (var pedido in Model.Pedidos) 
{
    @Html.Partial("~/Views/Shared/_Pedidos.cshtml", pedido)
}

@section Scripts {
    <script src="~/Scripts/Home.js"></script>
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.