Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax 通过Json ASP.NET MVC4返回HTML(包括图像)_Ajax_Json_Asp.net Mvc 4 - Fatal编程技术网

Ajax 通过Json ASP.NET MVC4返回HTML(包括图像)

Ajax 通过Json ASP.NET MVC4返回HTML(包括图像),ajax,json,asp.net-mvc-4,Ajax,Json,Asp.net Mvc 4,如何使用Ajax和Json返回部分视图结果进行查看,我使用的是ASP.NETMVC4结构。html还包括图像。在这里,我使用代码返回工作不正常的数据 return PartialView("_OnlineUsers", online_users); 我可以通过json以HTML(包括图像)的形式返回数据吗?像这样编写ajax调用 $('.btnSubmit').on('click', function(){ $.ajax({ url: '@Url.Action("Act

如何使用Ajax和Json返回部分视图结果进行查看,我使用的是ASP.NETMVC4结构。html还包括图像。在这里,我使用代码返回工作不正常的数据

return PartialView("_OnlineUsers", online_users);

我可以通过json以HTML(包括图像)的形式返回数据吗?

像这样编写ajax调用

$('.btnSubmit').on('click', function(){
    $.ajax({
        url: '@Url.Action("Action", "Controller")',
        type: 'post',
        cache: false,
        async: true,
        data: { id: "ID" },
        success: function(result){
            $('.divContent').html(result);
        } 
    });
});
在你的控制器上

public PartialViewResult Action(int id){
    var online_users = //populate your model
    return PartialView("_OnlineUsers", online_users);
}

不,不能使用相同的ajax请求返回html和json

$.get('@Url.Action("yourActionName","YOurControllerNme")',function(data){
$('#YourDivId').html(data);
});
服务器端代码中没有任何错误

要获得您可以使用的图像

<div class="display-field">
        <img src="@Url.Action("getPhotoAction", "ControllerName", new { id = Model.Id })" 
             title="@Model.TitleYouWantToSer"/>
    </div>