【问题标题】:@html.partial Model Not Found@html.partial 模型未找到
【发布时间】:2023-03-18 20:20:02
【问题描述】:

~/Views/PlanBuilder/PlanBuilder.cshtml

@{
ViewBag.Title = "PlanBuilder";
Layout = "~/Views/Shared/_LayoutPlanBuilder.cshtml";
}

<div id="tabs-1">

<div id="wrap" class="left">
<ul>
@Html.Partial("~/Views/Hall/Index.cshtml");
</ul>
</div>

</div>

~/Views/Hall/Index.cshtml

@model IEnumerable<ShaadiPlanner.Models.Hall>

@{
     ViewBag.Title = "Index";
     Layout = "~/Views/Shared/_LayoutPlanBuilder.cshtml";
    }

@foreach (var item in Model)  <!-- ERROR LINE -->
{
     <li id="@Html.DisplayFor(modelItem => item.ID)" class="options left">
          <div class="box-header">
               <h4 class="left"><span class="name">@Html.DisplayFor(modelItem => item.Name)</span> (@Html.DisplayFor(modelItem => item.Area))</h4>
               <h4 class="right">Rs. <span class="price">@Html.DisplayFor(modelItem => item.EstCost)</span></h4>
          </div>
     </li>
}

对象引用未设置为对象的实例。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

注意:当我像这样在@Html.Partial 代码中定义模型时,@Html.Partial("~/Views/Hall/Index.cshtml", model);它给出以下错误。

编译错误

描述:在编译服务此请求所需的资源期间发生错误。请查看以下特定错误详细信息并适当修改您的源代码。

编译器错误消息:CS0103:当前上下文中不存在名称“模型”

【问题讨论】:

  • 你应该尝试让这个问题更加具体和有针对性,而不是仅仅转储错误细节并且没有实际问题。

标签: razor model asp.net-mvc-4 partial-views renderpartial


【解决方案1】:

尝试大写模型,即

@Html.Partial("~/Views/Hall/Index.cshtml", Model);

作为@webdeveloper cmets,您还没有在视图中声明模型!

【讨论】:

  • 至少他需要在PlanBuilderpartial中定义模型
  • 您是否有加载视图的模型?您的代码中没有定义任何内容
猜你喜欢
  • 2015-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多