在mvc razor中更新两个局部视图的最佳方法

时间:2013-10-31 14:13:10

标签: c# jquery asp.net-mvc-4 razor

如何在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);

重复表格中的数据。

1 个答案:

答案 0 :(得分:0)

您应该在控制器中返回PartialView而不是View

return PartialView(viewModeltClasses);

如果使用View,MVC将渲染视图以及布局。如果使用PartialView,MVC将只呈现视图本身的内容。有关详细信息,请参阅this question