MVC4: is it possible with one Ajax call to controller action from script on razor view to get back partial view as html and Json format data ? Now I have two functions that make two trips to controller to two diff actions, each action calls same stored procedure to get data from db. I would like to combine it into one call.
MVC4:是否可以通过一个Ajax调用来自razor视图上的脚本的控制器操作来获取部分视图作为html和Json格式数据?现在我有两个函数,它们使两次访问控制器到两个diff操作,每个操作调用相同的存储过程来从db获取数据。我想将它合并为一个电话。
VIEW scripts section:
查看脚本部分:
function getPartialViewHtml(){
var val1=1;
$.ajax({
type:"POST",
url: 'Home/GetPartialViewHtml',
data: { parm1 : val1 },
success: function(htmlResult){
$('#divShowPartial').html(data);
getJsonResult(val1);
}
});
}
function getJsonResult(val1)
{
$.ajax({
type:"POST",
url: 'Home/GetJsonResult',
data: { parm1 : val1 },
success: function(jsonResult){
$('#txtShowJsonData1').val(data.text1);
$('#txtShowJsonData2').val(data.text2);
}
});
}
CONTROLLER
public PartialViewResult GetPartialViewHtml(int parm1)
{
PartialViewModel model = new PartialViewModel(parm1); // calls MyOtherModel model= new MyOtherModel (parm1);
return PartialView("MyPartialView", model);
}
public ActionResult GetJsonResult(intparm1)
{
MyOtherModel model= new MyOtherModel (parm1);
return Json(model);
}
MODELS
class PartialViewModel
{
public MyModel mm {get; set;}
public string otherData {get; set;}
........
}
class MyModel
{
public int id {get; set;}
public string text1 {get; set;}
public string text2 {get; set;}
}
1 个解决方案
#1
0
I don't think so, it is the nature of your return types.
我不这么认为,这是你的回归类型的本质。
Notice: PartialViewResult
and ActionResult
. You could switch ActionResult for JsonResult
since that makes more sense in your code.
注意:PartialViewResult和ActionResult。您可以为JsonResult切换ActionResult,因为这在您的代码中更有意义。
You could fire two synchronous AJAX calls instead of chaining their callbacks.
你可以触发两个同步的AJAX调用而不是链接它们的回调。
function getPartialViewHtml() {
var val1 = 1;
$.ajax({
type:"POST",
url: 'Home/GetPartialViewHtml',
data: { parm1 : val1 },
async: false,
success: function(htmlResult){
$('#divShowPartial').html(data);
}
});
$.ajax({
type:"POST",
url: 'Home/GetJsonResult',
data: { parm1 : val1 },
async: false,
success: function(jsonResult){
$('#txtShowJsonData1').val(data.text1);
$('#txtShowJsonData2').val(data.text2);
}
});
}
That saves your from declaring two separate functions.
这样可以避免声明两个单独的函数。
#1
0
I don't think so, it is the nature of your return types.
我不这么认为,这是你的回归类型的本质。
Notice: PartialViewResult
and ActionResult
. You could switch ActionResult for JsonResult
since that makes more sense in your code.
注意:PartialViewResult和ActionResult。您可以为JsonResult切换ActionResult,因为这在您的代码中更有意义。
You could fire two synchronous AJAX calls instead of chaining their callbacks.
你可以触发两个同步的AJAX调用而不是链接它们的回调。
function getPartialViewHtml() {
var val1 = 1;
$.ajax({
type:"POST",
url: 'Home/GetPartialViewHtml',
data: { parm1 : val1 },
async: false,
success: function(htmlResult){
$('#divShowPartial').html(data);
}
});
$.ajax({
type:"POST",
url: 'Home/GetJsonResult',
data: { parm1 : val1 },
async: false,
success: function(jsonResult){
$('#txtShowJsonData1').val(data.text1);
$('#txtShowJsonData2').val(data.text2);
}
});
}
That saves your from declaring two separate functions.
这样可以避免声明两个单独的函数。