Controller:
- public JsonResult Cars(int id)
- {
- JsonResult res = new JsonResult();
- Cars entity = car.Cars.SingleOrDefault(a => a.CarNo == id);
- res.Data = entity;
- return Json(res, JsonRequestBehavior.AllowGet);
- }
View:
- <script type="text/javascript">
- $("button").click(function () {
- $.getJSON("/Compare/Cars?id=7", null,function (res) {
- $("#name").html(res.CarNo);
- });
- });
- </script>
- <button>获得 JSON 数据</button>
- <div id= "name"></div>
不知道为什么,获取不到json,求大神解答一下