【问题标题】:RenderAction or RenderPartial dynamically动态渲染操作或渲染部分
【发布时间】:2014-09-19 05:59:58
【问题描述】:

我有一个包含以下代码的视图:

    @model  ComPost.Core.CommandsAndQueries.Contract.DataContract.DepositDetailDTO

@section scripts
{
    <script src="~/Scripts/DataTables-1.9.4/media/js/jquery.dataTables.js"></script>
    <script src="~/Scripts/jquery.datatables.bootstrap-pagination.js"></script>
    <script src="~/js/DepositDetail.js"></script>
}

    @Html.RenderAction(new { Action = "DepositDetailOverview", Controller = "Deposit" }, new { id = @Model.Id })

我的控制器有以下代码:

        public ActionResult DepositDetail(int id, int tabIndex = -1)
    {
        ViewBag.DepositId = id;
        ViewBag.ActionMethodForPartialView = this.GetControllerActionForTabIndex(tabIndex);
        DepositDetailDTO depositDetailDTO = this.QueriesServiceAgent.Call(s => s.GetDepositDetailForId(id));
        return View(depositDetailDTO);
    }

    public PartialViewResult DepositDetailOverview(int id)
    {
        ViewBag.DepositId = id;
        DepositOverviewScreenDTO depositOverviewScreenDTO = this.QueriesServiceAgent.Call(s => s.GetDepositOverviewForId(id));
        return PartialView(depositOverviewScreenDTO);
    }

    private string GetControllerActionForTabIndex(int tabIndex)
    {
        if (tabIndex <= 0)
        {
            return "DepositDetailOverview";
        }
        else if (tabIndex == 1)
        {
            return "DepositMailingLists";
        }
        return "DepositFinalize";
    }

当我们进入 DepositDetail 屏幕时,我们在控制器上调用“DepositDetail”方法。 这会调用 helper-method,该方法返回要调用的操作的名称以获取局部视图。

我似乎无法让它工作。 我错过了什么?

【问题讨论】:

  • 您能否将其缩短为您所看到的行为的最小示例?这是一个相当大的代码转储。
  • 我缩短了代码。我收到我的模型 ComPost.Core.CommandsAndQueries.Contract.DataContract.DepositDetailDTO 没有方法“RenderAction”的错误。
  • 我试过这条线:@{ Html.RenderPartial("DepositDetailOverview", new { id = @Model.Id });但后来我收到消息:传入字典的模型项的类型为“f__AnonymousType0`1[System.Int32]”,但该字典需要类型为“ComPost.Core.CommandsAndQueries.Contract.DataContract”的模型项.DepositOverviewScreenDTO'.
  • 我试过这样:@Html.RenderAction(new { Action = "DepositDetailOverview", Controller = "Deposit" }, new { id = @Model.Id }) 但后来我收到了消息:“System.Web.Mvc.HtmlHelper”不包含“RenderAction”的定义和最佳扩展方法重载“System.Web.Mvc.Html.ChildActionExtensions.RenderAction” (System.Web.Mvc.HtmlHelper, string, object)' 有一些无效参数
  • 接下来我这样尝试:@Html.RenderAction("DepositDetailOverview", new { id = @Model.Id }) 我得到了“System.Web.WebPages 的最佳重载方法匹配”。 WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' 有一些无效参数

标签: asp.net-mvc asp.net-mvc-partialview renderaction html.renderpartial


【解决方案1】:

好的,我的一位同事找到了解决方案。

解决方案在 js 文件中。 这应该如下所示:

    $(document).ready(function () {

    // Upon every page refresh, the tab with the current tab index is highlighted
    var currentTabIndex = $('#MainTabs').data("tabindex");
    $('#MainTabs li:eq('+ currentTabIndex +') a').tab('show');

    if (currentTabIndex == 1) {
        loadMailingListsForDepositTable();
    }
    if (currentTabIndex == 2) {
        LoadFinalizeInformation();
    }

    // wire up the tab clicked event, which requests a full page reload on the new tab index
    $('#MainTabs').click(function (e) {
        e.preventDefault();
        var nextTabIndex = $('#MainTabs li a:active').data('index');
        var depositId = $("#MainTabs").data("depositid");
        var dataSourceUrl = $("#MainTabs").data("datasourceurl").replace("-1", depositId).replace("-2", nextTabIndex);
        document.location.href = dataSourceUrl;
    });

});

var LoadFinalizeInformation = function () {
    document.getElementById('OverrideCheckox').onchange = function () {
        document.getElementById('OverrideReason').disabled = !this.checked;
        document.getElementById('DepositProductSelect').disabled = !this.checked;
    };
}

var loadMailingListsForDepositTable = function () {
    // do other stuff here.
}

对于部分视图,我们没有单独的 js 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 2012-02-18
    • 2021-08-28
    • 2011-09-26
    • 1970-01-01
    • 2017-05-07
    相关资源
    最近更新 更多