【发布时间】:2013-09-06 02:33:33
【问题描述】:
我正在尝试从我的视图中进行 ajax 调用,从我的控制器返回一个 json 对象。但是,当我尝试这个时,我得到了对象类型,而不是字符串形式的值。这是我的代码..
控制器
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult GetPerson()
{
var _model = _person;
return Json(_model, JsonRequestBehavior.AllowGet);
}
static Person _person = new Person()
{
FirstName = "Steve",
LastName = "Johnson",
Age = 27
};
}
查看
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-2.0.3.min.js"></script>
</head>
<body>
<div>
<form>
<fieldset>
<legend>The Person</legend>
<label>Name: </label>
<input />
<label>Age: </label>
</fieldset>
</form>
</div>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
url: '/Home/GetPerson',
type: 'GET',
success: function (result) { alert(result);}
});
});
</script>
警告框返回[object Object]。我正在尝试获取“Person”对象的字符串值。
任何帮助将不胜感激。
谢谢!
【问题讨论】:
标签: c# ajax asp.net-mvc razor-2