Loading Renderaction within _Layout

Asked

Viewed 174 times

1

How do I load data from a model inside in my _Layout? I have a standard layout for all views of my project where in this layout lies 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 take the photo and user name and the other of the system functionalities.

In the div where the user’s photo is like this:

<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 am making this change to try to pass the photo of the user at the time when the _layout 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>

Profiler

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}))
  • 2

    You can use partial view.

  • 2

    Thank you for your attention, but I was able to find the answer to my problem, link to https://social.msdn.microsoft.com/Forums/en-US/564e5e90-8277-4b1d-a23f-1f8ff16f4497/carregando-renderaction-dentro-de-layout?forum=mvcpt

  • 1

    thanks for your tip.

1 answer

4


Use @Html.Action.

Attention! Beware when working with @Html.Action. Codes can enter into loop or stop the entire application if it is misspelled.

Some tips:

1: Controller common

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

2: [ChildActionOnly]

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

3. Do not use async in Action

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

Browser other questions tagged

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