2

如何在 mvc razor 中更新两个局部视图的最佳方法?我需要从 Ajax 调用中更新 Multiple。这是更新后的正文,并且工作已正确完成。这种方式是真的吗?有没有更好的方法?我试过搜索但没有看到任何东西。我这样做如下:我正在使用这种方式:

我的观点 :

    @using (Ajax.BeginForm("Information", "Classes",
                            new AjaxOptions
                            {
                                 HttpMethod = "get",
                            }))
{


    <div id="Search">
        <input type="text" placeholder="Search" id="txtSearch" />
        <a id="StartSearch" href="javascript:void(0);" style="background-color: burlywood">Search</a>
    </div>

        <select id="dropDown">
            <option>10</option>
            <option>20</option>
            <option>30</option>
        </select>

    <div id="Paging">
        @Html.Partial("_PagingNumbers", Model.Page)
    </div>

    <div id="gridItems">
        @{ Html.RenderPartial("ClassesList", Model); }
    </div>

}

<script type="text/javascript">

$(document).ready(function () {

     $("#StartSearch").click(function () {
         changeContent('@Model.Page.CountRecordInPage', $("#txtSearch").val());
     });

     $("#dropDown").change(function () {
         changeContent($("#dropDown").val(), '@Model.Page.TextSearch');
     });

     function changeContent(count, search) {
         var currentUrl = '/' + '@Model.Page.Controller' + '/' + '@Model.Page.ActionName';
         var dataForChangeContent = {
             typeNo: '@Model.Page.ValueIdParametr',
             countryNo: '@Model.Page.CountryNo',
             count: count,
             search: search
         };
         $.ajax({
             url: currentUrl,
             data: dataForChangeContent,
             success: function (data) {
                 $('body').html(data);
             }
         });
     }

 });

部分视图 _PagingNumbers :

@model DGM.Common.Page


<script>
    function ChangeCurrentPage(obj) {
        var   page = $(obj).text();
    GoToPage(page);
}

function GoToPage(page) {

    var dataForChangeCurrentPage = {
        '@Model.NameIdParametr': '@Model.ValueIdParametr',
            countryNo: '@Model.CountryNo',
            count: '@Model.CountRecordInPage',
            search: '@Model.TextSearch',
            page: page
        };
        var urlChangeCurrentPage = '/' + '@Model.Controller' + '/' + '@Model.ActionName';
    $.ajax({
        url: urlChangeCurrentPage,
        data: dataForChangeCurrentPage,
        success: function (data) {
            $('body').html(data);
            var requestedUrl = this.url.replace(/[&?]X-Requested-With=XMLHttpRequest/i, "");
            history.pushState({ html: '' }, document.title, requestedUrl);
        }
    });
}

</script>
<div style="margin: 0 auto; text-align: center;">

    <div style="display: inline-table">

        @{
            var countPage = Model.CountPage;
            var currentPage = Model.CurrentPage;
        }
        @for (int i = 1; i < countPage + 1; i++)
        {

            <a class="scroll-content-item ui-widget-header" onclick=" ChangeCurrentPage(this) " href="javascript:void(0);">@i</a>
        }

    </div>


</div>

我的控制器:

 public ActionResult Information(short No, int count = 10,
        int page = 1, string search = "")
    {
        var Page = new Page();

        var  ClassesService = new ClassesService();

        var  viewModeltClasses = new  viewModeltClasses ();


        int totalCountRecord;

        if (string.IsNullOrWhiteSpace(search))
        {

                totalCountRecord =  ClassesService.GetCount(d => d.No == No);
                 viewModeltClasses .AirCraftClasseses =  ClassesService.GetMany(d => d.No == No,
                    page - 1,
                    count);

        }
        else
        {
            Page.IsSearch = true;
            Page.TextSearch = search;
            var predicateSearch = GetSearchPredicateAirCraftClasses(search, TypeSearch.Contains, No, country);
            var predicateOrder = GetOrderPredicateAirCraftClasses();
            totalCountRecord =  ClassesService.GetCountSearch(predicateSearch.Expand());

             viewModeltClasses .AirCraftClasseses =  ClassesService.Search(predicateSearch.Expand(), predicateOrder,
                page - 1, count);
        }


         viewModeltClasses .IsAllCountries = country == -1;
        Page.AllRecord = totalCountRecord;
        Page.CountPage = CommonMethods.GetCountPage(totalCountRecord, count);
        Page.NameIdParametr = "No";
        Page.ValueIdParametr = No;
        Page.CountRecordInPage = count;
        Page.ActionName = "ClassesInformation";
        Page.Controller = "Classes";
        Page.CurrentPage = page;

         viewModeltClasses .Page = Page;

        return View( viewModeltClasses );
    }

编辑 :

该程序正常工作。但我想知道这是否是一种系统的方式?我的身体真的变了吗?

我只需要更改表单中的数据。但它确实改变了整个身体的方式。

如果我编写以下代码:

$('form').html(data);

表格外的数据被重复。

4

1 回答 1

0

您应该在控制器中返回 aPartialView而不是 a :View

return PartialView(viewModeltClasses);

如果使用View,MVC 将与布局一起渲染视图。如果你使用PartialView,MVC 只会渲染视图本身的内容。有关更多信息,请参阅此问题

于 2013-10-31T14:27:48.263 回答