【问题标题】:Unable to load the data from other controller无法从其他控制器加载数据
【发布时间】:2018-01-18 10:33:44
【问题描述】:

我有一个控制器,它由许多其他部分视图组成。我想在另一个控制器中使用该控制器的特定部分。我能够看到设计,但无法在其中加载项目。

假设我在产品视图文件夹中有一个名为“产品”的控制器,我有_items.cshtml。我想在另一个名为“placeTheOrder”的控制器中使用这个_items.cshtml

div 的特定部分在 placeTheOrder 视图中我提到了@Html.Partial("~/Views/Products/_items.cshtml")。即使这样做了,它也无法将来自_items.cshtml 的内容加载到 placeTheOrder。

我做错了什么。

_items.cshtml 查看

    <div id="accordionProduct" class="span-6 last prod-acc">
        @Html.Partial("~/Views/product/_ezCpSearchBar.cshtml")
        <div id="filterPanel" class="span-6 last filter-panel">
            <div class="span-6 last">
                <div class="filter-panel-head">
                    <h1>Filter</h1>
                </div>
            </div>
            <div class="span-6 last">
                <div class="filter-panel-body">
                    &nbsp;
                    <div class="filter-panel-prop"></div>
                </div>
            </div>
        </div>
        <div id="accordionProductInner" class="span-6 last prod-acc-body">
        </div>
    </div>
    // this is the script are where the content gets loaded into the view
    <div id="TemplateFilterItem" class="hidden"></div>
    <div id="TemplateLastViewItem" class="hidden"></div>

这是我在另一个控制器中引用它的地方

<div class="span-6" style="background-color:#d4d4d4;padding:20px;">     
    @Html.Partial("~/Views/product/_items.cshtml")
</div>

【问题讨论】:

  • 那么您是否想仅将部分视图加载为 html 而无需通过控制器操作?
  • 您可以将代码添加到问题中吗?
  • 是的,正是@AmrElgarhy。我可以在页面上看到 html,但我还需要在其中加载内容。
  • 您可以看到哪些 html 以及要加载哪些?我不清楚,添加一些代码会很有帮助
  • 我添加了代码@AmrElgarh,

标签: asp.net asp.net-mvc asp.net-mvc-3


【解决方案1】:

您可以在视图中改用 Html.Renderpartial

Html.RenderPartial("~/Views/ControllerName/ViewName.cshtml", ModelData);

如果您对两个视图使用不同的模型,那么您需要制作包含所需属性的 ViewModel。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    相关资源
    最近更新 更多