C# 无法对视图中的空引用执行运行时绑定
我有一个共享视图,根据计划是否存在隐藏或显示标题。但是,如果(Model!=null&&Model.Plan>0)这一行出现错误“无法对null引用执行运行时绑定”,我不确定原因。我错过了什么 这是我的密码:C# 无法对视图中的空引用执行运行时绑定,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我有一个共享视图,根据计划是否存在隐藏或显示标题。但是,如果(Model!=null&&Model.Plan>0)这一行出现错误“无法对null引用执行运行时绑定”,我不确定原因。我错过了什么 这是我的密码: @using System.SharedConfig @{ var plan = ConfigurationManager.AppSettings["planString"]; } <ul class="nav nav-pills nav-justified internal
@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>
@使用System.SharedConfig
@{
var plan=ConfigurationManager.AppSettings[“planString”];
}
- @Html.ActionLink(“保险”、“主”、“保险”)
@if(Model!=null&&Model.Plan>0)
{
- @Html.ActionLink(plan,“Main”,“plan”)
}
其他的
{
//RTPlan链接隐藏,因此根据设计调整标题
.nav对正>li>a{
文本对齐:左对齐;
左侧填充:88px!重要;
}
}
您没有在视图中加载模型,因此视图不知道您请求的是哪个模型。当您从控制器返回视图时,可以在return语句中返回模型,如
return View(model);
在视图中添加该模型
像
Model.Plan
是否可为空?否。它不可为空@Model
声明在哪里?这是在共享视图中。所以我没有补充。
@model ModelName