Ajax 通过Json ASP.NET MVC4返回HTML(包括图像)
如何使用Ajax和Json返回部分视图结果进行查看,我使用的是ASP.NETMVC4结构。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
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>