简体   繁体   English

在MVC剃刀中更新两个局部视图的最佳方法

[英]best way for update two partial view in mvc razor

how to best way for update two partial view in mvc razor ? 如何最好的方式来更新MVC剃刀中的两个局部视图? I need to update Multiple from an Ajax call.Here is the updated body And the work is done properly. 我需要从Ajax调用更新Multiple。这是更新的正文,并且工作已正确完成。 Is this way true? 这是真的吗? Is there a better way?I've tried searching but did not see anything. 有没有更好的方法?我尝试搜索但没有看到任何东西。 I did it as follows: I am using this way : 我这样做如下:我正在使用这种方式:

my view : 我的观点 :

    @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);
             }
         });
     }

 });

partial view _PagingNumbers : 部分视图_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>

my controller : 我的控制器:

 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 );
    }

edit : 编辑:

The program works correctly. 该程序正常工作。 But I want to know whether this is a systematic way? 但是我想知道这是否是系统的方法? I have certainly changed my body? 我肯定改变了我的身体?

I just need to change the data within the form. 我只需要更改表单中的数据。 But it does change the way the entire body. 但这确实改变了整个身体的方式。

If I write the following code: 如果我编写以下代码:

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

Data out of the form is repeated. 重复表格以外的数据。

You should return a PartialView in stead of a View in the controller: 您应该返回PartialView而不是控制器中的View

return PartialView(viewModeltClasses);

If you use View , MVC will render the view along with the layout. 如果使用View ,则MVC将渲染视图以及布局。 If you use PartialView , MVC will just render the content of the view itself. 如果使用PartialView ,则MVC只会渲染视图本身的内容。 See this question for more info. 有关更多信息,请参见此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM