Webforms应用程序中的布局页面中的MVC部分视图给出错误[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我有一个ASP.NET webforms应用程序,我创建了一个MVC视图和一个布局页面,我也创建了一个部分视图,但它在布局页面加载时出错。请参阅以下错误和我的代码:

_LayoutPage.cshtml出错

我在下面添加了我的代码:

_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(部分视图)

@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; }       
    }
}
c# asp.net asp.net-mvc webforms
1个回答
0
投票

当您在控制器中返回部分视图时,您必须传递模型

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

那会做的

© www.soinside.com 2019 - 2024. All rights reserved.