热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在局部视图中渲染Json数据-RenderJsondatainpartialview

IamcurrentlyworkingonashoppingcartapplicationusingMVC5andhavearequirementtoallowedi

I am currently working on a shopping cart application using MVC5 and have a requirement to allow editing of the cart which is displayed as a partial view in a jQuery UI dialog.

我目前正在使用MVC5开发购物车应用程序,并且要求允许编辑在jQuery UI对话框中显示为部分视图的购物车。

I have added a Javascript method to allow an ajax call to the controller however I am having an issue with the display of the json result data. At the moment it is rendering as a blank page even though the json data is valid.

我添加了一个Javascript方法,允许对控制器进行ajax调用,但是我遇到了json结果数据显示的问题。目前,即使json数据有效,它也会呈现为空白页。

The jquery code is as follows:

jquery代码如下:

$(".RemoveLink").click(function () {
        // Get the id from the link
        var recordToDelete = $(this).attr("cartid");

        $.ajax({
            url: "/ShoppingCart/RemoveFromCart/",
            type: 'POST',
            datatype: 'html',
            data: {
                id: recordToDelete
            },
            success: function (data) {
                $("#shoppingcart").html(data);                    
            },
            error: function (jqXHR, textStatus, errorstring) {
                alert("There has been an error textStatus: [" + textStatus + "] \r\n errorstring: [" + errorstring + "]");
            }
        })
    });

I have attempted to display the data manually and this works successfully however I was hoping I could do it using my partial view as it stands.

我试图手动显示数据,这成功地工作,但我希望我可以使用我的部分视图来实现它。

Thanks,

Stuart

2 个解决方案

#1


0  

Why don't you have your RemoveFromShoppingCart Controller return the partial view you want updated (bound with the updated model). Then in Jquery simply update the section that shows the partial view with the updated Partial View.

你为什么不让你的RemoveFromShoppingCart控制器返回你想要更新的部分视图(与更新的模型绑定)。然后在Jquery中,只需使用更新的部分视图更新显示局部视图的部分。

I am assuming that in your current code sample data is a JSON object that is coming back from the server.

我假设您当前的代码示例数据是一个从服务器返回的JSON对象。

#2


0  

You need to specify the required return type:

您需要指定所需的返回类型:

if you want JSON, specify:

如果你想要JSON,请指定:

datatype: 'json',

As your code seems to want HTML and not JSON, do not return a JSON result.

由于您的代码似乎需要HTML而不是JSON,因此不要返回JSON结果。

Please also show your controller action method

还请显示您的控制器操作方法


推荐阅读
author-avatar
林志群晴梦
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有