Loading RenderAction inside _Layout

1

How do I load data from a model into my _Layout ? I have a default layout for all views of my project where in this layout is the div where is the user photo that is in my application at the moment, so I would have to work with 2 models at the same time, one to get the photo and name of the user and the other of the system's features.

In%% of where the user's photo is:

<div class="user-img-div user-basic basic-perfil-borda">
    <img src="/Content/NewTheme/img/user.png" class="img-thumbnail" />

    <div class="inner-text">
        Jhon Deo Alex 
        <br />
        <small>Last Login : 2 Weeks Ago </small>
    </div>
</div>

I'm making this change to try to pass the user's photo when div is called:

<div class="user-img-div user-basic basic-perfil-borda">
    <img src="@Html.RenderAction("Partial1","Perfil")" class="img-thumbnail" />

    <div class="inner-text">
        Jhon Deo Alex 
        <br />
        <small>Last Login : 2 Weeks Ago </small>
    </div>
</div>

ProfileController

using IntranetCBL.Models.Persistencia;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace IntranetCBL.Controllers
{
    public class PerfilController : Controller
    {
        Usuarios user = new Usuarios();
        DataContext db = new DataContext();
        // GET: /Perfil/
        public ActionResult Partial1()

        {

            var imagemlogo = db.usuarios.Where(p => p.Id == 1);
            user.UrlImg = imagemlogo.ToString();
            return View(user.ToString());
        }
    }
}

Partial1.cshtml:

@model IntranetCBL.Usuarios


@foreach (var item in Model)
{
    @item.UrlImg
}
@{ Layout = null; }
<h1>teste</h2>

My application will start with HomeController :

public class HomeController : Controller
{

    DataContext db = new DataContext();
    CRUD cru = new CRUD();

    public ViewResult Index(int? pagina) {
        int tamanhoPagina = 3;
        int numeroPagina = pagina ?? 1;
        return View(db.avisos.OrderByDescending(p=> p.Id).ToPagedList(numeroPagina,tamanhoPagina));
    }

Index.cshtml:

@model PagedList.IPagedList<IntranetCBL.Avisos>
@using PagedList.Mvc
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@foreach (var item in Model)
{
    @*<div class="container-fluid">*@
        <div class="panel panel-primary">
            <div class="panel-heading">
            <h3>@item.Titulo</h3>
            </div>

            <div class="panel-body">

                    <div class="row">
                        <div class="col-md-10">
                            @item.Texto
                           </div>
                        </div>
                    <div class="row">
                        <div class="col-md-10">
                       <img src="@item.UrlImg" class="img-responsive" />
                        </div>
                    </div>


            </div>
            <div class="panel-footer">
                <div class="row">
                    <div class="col-md-3">
                        Autor: @item.usuario.Nome
                    </div>
                    <div class="col-md-3">
                       @item.DataAviso
                    </div>

                </div>

            </div>
        </div>
    @*</div>*@

}</br>
Pagína: @Model.PageNumber de @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index", new { pagina = page}))
    
asked by anonymous 28.11.2016 / 17:28

1 answer

4

Use @Html.Action .

Attention! Be careful when working with @Html.Action . Codes can enter loop or stop the entire application if they are spelled wrong.

Some tips:

1:% common%

public abstract class Controller : System.Web.Mvc.Controller
{
    ...
}

2: Controller

public abstract class Controller : System.Web.Mvc.Controller
{
    [ChildActionOnly]
    public ActionResult DadosUsuario()
    {
        ...
        return PartialView("_DadosUsuario", umViewModelComAFoto);
    }
}

3. Do not use [ChildActionOnly] in Action

ASP.NET MVC5 does not work well with async in the case of Child Actions . Use the traditional synchronous method.

    
28.11.2016 / 18:12