用ajax获取后台数据,返回json数据,在前台使用
后台:publicclassHomeController:Controller{//GET:HomepublicActionResultIndex(int?id){var...
后台:
public class HomeController : Controller
{
// GET: Home
public ActionResult Index(int? id)
{
var Id = id == null ? 0 : Convert.ToInt32(id);
ViewData["people"] = setInfo(Id);
if(Id == 0)
return View();
else
return Json(new { person = ViewData["people"] });
}
public People setInfo(int id){ }
}
public class People
{
public string name { get; set; }
public string sex { get; set; }
public int age { get; set; }
}
前台:
@using test.Controllers;
@{
ViewBag.Title = "Index";
People people = (People)ViewData["people"];
}
<script language="javascript">
$(function () {
$("#click").click(function () {
$.ajax({
type: "GET",
url: "Index?id=1",//请求的后台地址
data: null,//前台传给后台的参数
success: function (msg) {//msg:返回值
}
});
});
});
</script>
<h2>ajax test</h2>
<input type="text" name="name" value="@people.name" />
<input type="text" name="sex" value="@people.sex" />
<input type="text" name="age" value="@people.age" />
<input type="button" value="点击" id="click"/> 展开
public class HomeController : Controller
{
// GET: Home
public ActionResult Index(int? id)
{
var Id = id == null ? 0 : Convert.ToInt32(id);
ViewData["people"] = setInfo(Id);
if(Id == 0)
return View();
else
return Json(new { person = ViewData["people"] });
}
public People setInfo(int id){ }
}
public class People
{
public string name { get; set; }
public string sex { get; set; }
public int age { get; set; }
}
前台:
@using test.Controllers;
@{
ViewBag.Title = "Index";
People people = (People)ViewData["people"];
}
<script language="javascript">
$(function () {
$("#click").click(function () {
$.ajax({
type: "GET",
url: "Index?id=1",//请求的后台地址
data: null,//前台传给后台的参数
success: function (msg) {//msg:返回值
}
});
});
});
</script>
<h2>ajax test</h2>
<input type="text" name="name" value="@people.name" />
<input type="text" name="sex" value="@people.sex" />
<input type="text" name="age" value="@people.age" />
<input type="button" value="点击" id="click"/> 展开
1个回答
展开全部
然后呢?你想问什么问题,百度可以贴代码,你这上来一坨谁还有心情看。
追答
刷新值:可以在success回调中,
$("#name").val(msg);
保存值:ajax参数你可以把数据传回后台,让后台做处理
data:{name:$("#name").val(),sex:$("#sex").val(),age:$("#age").val()}
关于提问贴代码,你既然可以在提问中贴图片,为什么不把代码截图一下贴上呢 ?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询