无法对布局上的空引用执行运行时绑定

本文关键字:引用 执行 运行时 绑定 布局 | 更新日期: 2023-09-27 18:35:56

它的mvc 4.有人能帮我吗?!

控制器:

public ActionResult Order(int id, int hotelId)
{
    ViewBag.PackageFrames = packageRepo.GetPackageFrames(id);
    ViewBag.PackageId = id;
    ViewBag.HotelId = hotelId;
    return View();
}

ViewBag.PackageFrames有三条记录。

视图:

@{
    ViewBag.Title = "Range";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<section class="header-page fade-up" style="background: url(/Images/Packages/Backgrounds/@Model.Background) 50% 0 fixed;">
    <div class="bounce-in animate4">
        <h1 class="header-pagetitle bye">@Model.Name</h1>
    </div>
</section>
<section id="internalpage">
    <div class="container clearfix">
        <div class="grid_12">
            @{List<Paristan.AgencyPortal.Model.Packages.PackageFrame> packageFrames = (List<Paristan.AgencyPortal.Model.Packages.PackageFrame>)ViewBag.PackageFrames;}
            <select id="packageFrame" packageId="@ViewBag.PackageId" hotelId="@ViewBag.HotelId">
                @foreach (var packageFrame in packageFrames)
                {
                    <option value="@packageFrame.PackageFrameId">@packageFrame.FromDate - @packageFrame.ToDate</option>
                }
            </select>
        </div>
    </div>
</section>

_ViewStart

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

并且_Layout位于~/Views/Shared文件夹中,没有任何内容为空。 但我Layout = "~/Views/Shared/_Layout.cshtml";行上看到以下错误。

无法对空引用执行运行时绑定

无法对布局上的空引用执行运行时绑定

你必须在foreach中使用ViewBag.packageFrames。

   @{
        ViewBag.Title = "Range";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

<section class="header-page fade-up" style="background: url(/Images/Packages/Backgrounds/@Model.Background) 50% 0 fixed;">
    <div class="bounce-in animate4">
        <h1 class="header-pagetitle bye">@Model.Name</h1>
    </div>
</section>
<section id="internalpage">
    <div class="container clearfix">
        <div class="grid_12">
            @{List<Paristan.AgencyPortal.Model.Packages.PackageFrame> packageFrames = (List<Paristan.AgencyPortal.Model.Packages.PackageFrame>)ViewBag.PackageFrames;}
            <select id="packageFrame" packageId="@ViewBag.PackageId" hotelId="@ViewBag.HotelId">
                @foreach (var packageFrame in ViewBag.packageFrames)
                {
                    <option value="@packageFrame.PackageFrameId">@packageFrame.FromDate - @packageFrame.ToDate</option>
                }
            </select>
        </div>
    </div>
</section>

在 .cshtml 中有 @Model.Name,在控制器中没有定义模型