它的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
文件夹中,没有任何内容为null。但我在Layout = "~/Views/Shared/_Layout.cshtml";
行看到以下错误。
无法对空引用执行运行时绑定
答案 0 :(得分:0)
您必须在foreach中使用ViewBag.packageFrame。
@{
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>
答案 1 :(得分:0)
你在.cshtml中有@ Model.Name,在控制器中没有定义模型