0

I have an ASP.NET webforms application and I have created a MVC View and a Layout Page and I have also created a Partial View but it is giving an error while loading in Layout Page. Please see the following error and my code:

Error on _LayoutPage.cshtml

Error on _LayoutPage.cshtml

I have added my code bellow:

_LayoutPage.cshtml

<!DOCTYPE html>    
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
</head>
<body>
    <p>Bellow table is in Layout page(Master Page)</p>
    <table border="1" cellpadding="5" cellspacing="5">
        <tr>
            <td>@{Html.RenderAction("_Header", "Layout"); }</td>
            <td>@RenderBody()</td>
        </tr>
    </table>
</body>
</html>

LayoutController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CBT_WebApplication.Library.CS;
using CBT_WebApplicationProject.Models;

namespace CBT_WebApplicationProject.Controllers
{
    public class LayoutController : Controller
    {
        public ActionResult _Header()
        {
            HeaderVM hvm = new HeaderVM();
            hvm.sAltText = "HFI Logo";
            hvm.sHomeURL = "~/Home3.aspx";
            hvm.sClass = "logo-header";
            hvm.sLogoSrc = "/images/graphics/hf-logo-header-blue.png";

            return PartialView("~/Views/Header/_Header.cshtml");
        }
    }
}

_Header.cshtml (Partial View)

@model CBT_WebApplicationProject.Models.HeaderVM

    <div id="divHFI" class='@Model.sClass'>
        <a href='@Model.sHomeURL'>
            <img src='@Model.sLogoSrc' alt='@Model.sAltText' />
        </a>
    </div>

HeaderVM.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CBT_WebApplicationProject.Models
{
    public class HeaderVM
    {
        public string sAltText { get; set; }
        public string sHomeURL { get; set; }
        public string sLogoSrc { get; set; }
        public string sClass { get; set; }       
    }
}
Baber ali
  • 95
  • 8
  • @VDWWD My question is not specifically related to NullReferenceException Please review my question again as this answer (https://stackoverflow.com/a/55294184/8561847) has solved my problem. Thanks – Baber ali Mar 22 '19 at 10:15

1 Answers1

0

You have to pass your model when you return PartialView in controller in your case its

return PartialView("~/Views/Header/_Header.cshtml",hvm);

that will do it

Haroon nasir
  • 582
  • 6
  • 21