【问题标题】:Partial View content is not reloaded不重新加载部分视图内容
【发布时间】:2015-12-23 22:03:42
【问题描述】:

我有一个带有按钮的视图,当单击此按钮时,ajax 函数会调用控制器方法 ApplicationAndUse,该方法应该将列表传递给包含在我的视图中的局部视图。部分视图内容应该被刷新,但这不起作用,我的部分仍然是空的。 我的代码:

主视图:

@model List<String>


<div class="row">
<div class="col-md-2">
    @foreach (var item in Model)
    {
        <div id="univers-@item" class="btn btn-info">@item</div><br />
    }
</div>
<div class="col-md-10">
    @Html.Partial("_ApplicationAndUsePartial", null, new ViewDataDictionary())
</div>
</div> 
@section scripts
{
<script type="text/javascript">

    $(function () {

        $('[id^=univers]').click(function () {

            var selectedButton = $(this).attr('id');
            var selectedUniverse = selectedButton.substring(selectedButton.indexOf('-') + 1, selectedButton.lenght);


            $.ajax({
                url: "http://" + window.location.host + "/UseAndNeed/ApplicationAndUse",
                type: "POST",
                data: { idUniverse: selectedUniverse },
                dataType: "json",
            });
        });
    });
</script>
}

局部视图:

@model List<int>

@if (Model!= null) { 
foreach (var item in Model)
{
    <div id="ApplicationUse-@item" class="btn btn-default">@item</div><br />
}
}

控制器功能:

[OutputCache(Duration = 0)]
    public ActionResult ApplicationAndUse(String idUniverse)
    {
         List<int> items = new List<int>();
         items.Add(1);
         items.Add(2);
        return PartialView("_ApplicationAndUsePartial", (object)items);
    }

我错过了什么?

【问题讨论】:

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


    【解决方案1】:

    为我们要显示部分视图内容的 div 提供一个唯一的 Id。

    <div id="myPartial" class="col-md-10">
        @Html.Partial("_ApplicationAndUsePartial", null, new ViewDataDictionary())
    </div>
    

    在 ajax 方法的 success 处理程序中,使用来自 ajax 调用的响应更新此 div 的 innerHTML。此外,您在进行 ajax 调用时也不需要指定 dataType 值。

    var myUrl= "http://" + window.location.host + "/UseAndNeed/ApplicationAndUse";
    
    $.ajax({  type: "POST",
              url : myUrl,
              data: { idUniverse: selectedUniverse },
              success:function(result){
                  $("myPartial").html(result);
              }
           });
    

    您始终应该使用Url.Action 或Url.RouteUrl html 辅助方法来构建操作方法的url。无论您当前的页面/路径如何,它都会正确构建 url。

    var myUrl= "@Url.Action("ApplicationAndUse","UseAndNeeed")";
    

    如果您的 js 代码在 razor 视图中,则此方法有效。但是,如果您的代码位于单独的 javascript 文件中,您可以使用上述帮助方法在剃刀视图中构建 url,并将其保存在外部 js 文件代码可以访问的变量中。这样做时始终确保使用 javascript 命名空间以避免全局 javascript 变量可能出现的问题。

    @section Scripts
    {
     <script>
        var myApp = myApp || {};
        myApp.Urls = myApp.Urls || {};
        myApp.Urls.baseUrl = '@Url.Content("~")';       
     </script>
     <script src="~/Scripts/PageSpecificExternalJsFile.js"></script>
    
    }
    

    在您的PageSpecificExternalJsFile.js 文件中,您可以阅读它。

    var myUrl= myApp.Urls.baseUrl+"UseAndNeed/ApplicationAndUse";
    $("#myPartial").load(myUrl+'?idUniverse=someValue');
    

    【讨论】:

    • 是的,以更多MVC方式加载partialView的其他方式是调用load:$("#myPartial").load('@(Url.Action("UseAndNeed","ApplicationAndUse",null, Request.Url.Scheme))?idUniverse=' + selectedUniverse);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多