C# MVC BeginForm-从控制器返回三个不同的ajax文本字符串
我需要从控制器向视图发送3个变量(viewbags),当数据在beginform中提交时。由于下面的AJAX函数,目前我只能得到一个要发回的变量 JQUERY/AJAXC# MVC BeginForm-从控制器返回三个不同的ajax文本字符串,c#,jquery,ajax,asp.net-mvc,C#,Jquery,Ajax,Asp.net Mvc,我需要从控制器向视图发送3个变量(viewbags),当数据在beginform中提交时。由于下面的AJAX函数,目前我只能得到一个要发回的变量 JQUERY/AJAX function autosubmit() { $.ajax({ type: 'POST', url: this.action, data: $('form').serialize(), success: fu
function autosubmit() {
$.ajax({
type: 'POST',
url: this.action,
data: $('form').serialize(),
success: function (result) {
$('#one').html(result); //ViewBag.one
$('#two').html(result); //ViewBag.two
$('#three').html(result); //ViewBag.three
}
});
}
表格:
忘记ViewBag/ViewData。就好像它从未存在过一样 使用JSON:
[HttpPost]
public ActionResult Index(model stuff)
{
var data = new
{
data1 = stuff.data1,
data2 = stuff.data2,
data3 = stuff.data3
};
return Json(data);
}
然后消费:
$.ajax({
type: 'POST',
url: this.action,
data: $('form').serialize(),
success: function (result) {
$('#one').html(result.data1);
$('#two').html(result.data2);
$('#three').html(result.data3);
}
});
[HttpPost]
public ActionResult Index(model stuff)
{
var data = new
{
data1 = stuff.data1,
data2 = stuff.data2,
data3 = stuff.data3
};
return Json(data);
}
$.ajax({
type: 'POST',
url: this.action,
data: $('form').serialize(),
success: function (result) {
$('#one').html(result.data1);
$('#two').html(result.data2);
$('#three').html(result.data3);
}
});