0

I have Partial view LeftBanner.cshtml that should display some data from DB (images)

LeftBanner.cshtml

@model IEnumerable<WebApplication1.ViewModels.Banner>

    <table class="table">


   <tr>
        <th>Test Banner!</th>
    </tr>

    @foreach (var banner in Model)
    {
        <tr>
            <td>
                @Html.LabelFor(b => banner.ImageDesc);
                <img src="@Url.Content(banner.ImagePath)" alt="Image" />
            </td>
        </tr>
    }

</table>

SharedController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.ViewModels;

namespace WebApplication1.Controllers
{
    public class SharedController : Controller
    {   
        public ActionResult LeftBanner()
        {
            List<Banner> b = new List<Banner>() {
                new Banner() {ImageDesc = "Left Banner Description", ImagePath = "~/Images/money.jpg" },
                new Banner() {ImageDesc = "Left Banner Description2", ImagePath = "~/Images/money.jpg" }
            };
             return View(b);
           // return View();
        }
    }
}

_Layout.cshtml

<!DOCTYPE html>
<html>
<head>

</head>
<body>
    <div>
        @RenderPage("~/Views/Shared/LeftBanner.cshtml") 
    </div>

</body>
</html>

The Error

The model item passed into the dictionary is of type 'WebApplication1.ViewModels.EmployeeListViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[WebApplication1.ViewModels.Banner]'.

In Asp.net Web-forms ascx UserControls can be independent and really reusable.

Please help my to understand, why i can't encapsulate my partial view? It is not real reuse, If I have to pass to partial view model every time I use it. Can it be independent and go to a database without passing it a model?

Thanks a lot!

Vadim M
  • 3,155
  • 2
  • 10
  • 13
  • Add this on top of your Layout page @model WebApplication1.ViewModels – Basanta Matia Apr 06 '17 at 06:29
  • Hello, I add @model WebApplication1.ViewModels.Banner to Layout page. Now i get onother error: The model item passed into the dictionary is of type 'WebApplication1.ViewModels.EmployeeListViewModel', but this dictionary requires a model item of type 'WebApplication1.ViewModels.Banner'. – Vadim M Apr 06 '17 at 06:36
  • Check my bellow answer and let me know. – Basanta Matia Apr 06 '17 at 06:43

1 Answers1

3

In your LeftBanner() Action method return like this,

return PartialView("LeftBanner",b);

Then in your _Layout.cshtml page, call the action LeftBanner like bellow,

@Html.Action("LeftBanner", "Shared")

Hope it helps :)

Basanta Matia
  • 1,504
  • 3
  • 15
  • 27