无法对视图中的空引用执行运行时绑定

时间:2018-11-27 13:07:18

标签: c# asp.net asp.net-mvc

我有一个共享视图,该视图隐藏或显示基于计划的标题是否存在。但是我在此行@if (Model != null && Model.Plan > 0)上收到此错误“无法对空引用执行运行时绑定”,不确定原因。我在这里想念什么?

这是我的代码:

@using System.SharedConfig
@{
    var plan = ConfigurationManager.AppSettings["planString"];
}
<ul class="nav nav-pills nav-justified internal-nav">
<li data-navigation-pill="insurance">@Html.ActionLink("Insurance", "Main", "Insurance")</li>
@if (Model != null && Model.Plan > 0)
{
    <li data-navigation-pill="plan">@Html.ActionLink(plan, "Main", "Plan")</li>
}
else
{
    //The RTPlan link is hidden, so adjust the header according to the design
    <style type="text/css">
        .nav-justified > li > a {
            text-align: left;
            padding-left: 88px !important;
        }
    </style>
}
</ul>

1 个答案:

答案 0 :(得分:1)

您没有在视图中加载模型,因此视图不知道您要的模型。从控制器返回视图时,可以在return语句中返回模型,例如

return View(model);

在您的视图中添加该模型。

@model ModelName