简体   繁体   English

Razor Pages - 在 ajax 成功时重新加载部分视图

[英]Razor Pages - reload Partial View on ajax success

In my view (let's name this view - Main/Index) I have list with some items on the left side (used Component), and main div on the right side, which has partial view inside.在我的视图中(让我们将此视图命名为 - Main/Index),我在左侧列出了一些项目(使用的组件),在右侧列出了主 div,其中包含部分视图。 When I click some item on the left side, I want to send parameter to the method in Main/Index class, process this data and send it back as json, but also refresh the partial at the same time.当我单击左侧的某个项目时,我想将参数发送到 Main/Index 类中的方法,处理此数据并将其作为 json 发送回来,但同时也刷新部分。 Partial is getting data from this view.部分正在从此视图获取数据。 Really, I don't know the way I try to do this is good, maybe will you have some suggestions for me?真的,我不知道我尝试这样做的方式是否好,也许你能给我一些建议? Would be great.会很好。

Anyway my code is: Main/Index.cshtml无论如何我的代码是:Main/Index.cshtml

<div class="row">
    <div class="col-3">
        <div class="nav flex-column nav-pills" id="v-pills-tab" role="tablist" aria-orientation="vertical">
            @await Component.InvokeAsync("DisplayBar")
        </div>
    </div>
    <div class="col-9">
        <div id="mainDiv">       
            <partial name="~/Pages/Shared/_MainContent.cshtml" />
        </div>
    </div>
</div>

Main/Index.cshtml.cs主/索引.cshtml.cs

public class IndexModel : PageModel
{

    public string ParamValue{ get; set; }

    public void OnGet() {}

    public JsonResult OnPostRefreshMain(string paramValue)
    {
        ParamValue= DictionaryHelper.TranslatedParamsDict[paramValue];

        return new JsonResult(ParamValue);
    }
}

Shared/_MainContent.cshtml (partial) Shared/_MainContent.cshtml(部分)

@model Pages.Main.IndexModel

@{
    var paramValue = string.IsNullOrEmpty(Model.ParamValue) ? "No param" : Model.ParamValue;
}

<h5>@paramValue selected</h5>

js script js脚本

$(document).on('click', 'a.nav-link.appsList-element:not(.active)', function () {
    var paramValueResult = 'some id';
    $.ajax({
        url: '/Main/Index?handler=RefreshMain',
        type: 'post',
        dataType: 'json',
        data: {
            paramValue: paramValueResult
        },
        beforeSend: function (xhr) {
            xhr.setRequestHeader("XSRF-TOKEN",
                $('input:hidden[name="__RequestVerificationToken"]').val());
        },
        success: function (obj) {
            $('#mainDiv').load('/Shared/_MainContent.cshtml');
        }
    });
});

Now js is calling my method fine, method is returning translated data, but the partial is not reloading.现在 js 调用我的方法很好,方法正在返回翻译的数据,但部分没有重新加载。

In the future I will get many data depends on this paramValue - I will display statistics of product related to this paramValue, so I'm not sure my solution is ok and maybe is better way to do this?将来我将获得许多取决于此 paramValue 的数据 - 我将显示与此 paramValue 相关的产品统计信息,所以我不确定我的解决方案是否可行,也许是更好的方法?

you have to populate view with jquery you get the json from controller in success of ajax so <h5>@paramValue selected</h5> this should have new value that is returned so you can use some sort of identity to h5 element and change it's content with new value so in the success function $('h5'),html('<h5>'+what you want to show here+' selected</h5>') and you received the json in obj you can see what is in obj by console.log(obj);您必须使用 jquery 填充视图,您才能在 ajax 成功时从控制器获取 json,因此<h5>@paramValue selected</h5>这应该具有返回的新值,以便您可以对 h5 元素使用某种身份并更改它内容具有新值,因此在成功函数$('h5'),html('<h5>'+what you want to show here+' selected</h5>')你收到了 obj 中的 json 你可以看到什么是在 obj 中通过console.log(obj); in success function in short you can send the whole html from controller and just replace the html content $('#parentDivId').html(data);简而言之,在成功功能中,您可以从控制器发送整个 html 并替换 html 内容$('#parentDivId').html(data); so in this case ajax data type should be 'text/html' or you can get the json and put json content where ever you want which been described earlier btw I said you can transfer whole page so in this case controller should return html (return type should be accordingly could be View)所以在这种情况下,ajax 数据类型应该是'text/html'或者你可以获取 json 并将 json 内容放在你想要的任何地方,前面已经描述过顺便说一句,我说你可以传输整个页面,所以在这种情况下,控制器应该返回 html(返回类型应该相应地可以是视图)

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

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