【发布时间】:2013-11-24 07:00:52
【问题描述】:
我在下面的代码中使用 ajax 调用部分视图,但是当我单击产品名称的链接时,不会通过 ajax 检索到该产品的描述,并且会执行 ajax 错误。我正在检索用户在同一页面上选择的项目的详细信息,但未检索到。请给出任何建议,因为我是 MVC 新手,所以问题出在哪里。谢谢...
创建.cshtml
@model List<PartialView.Models.tbl_product>
<!DOCTYPE html>
<html>
<head>
<title>Create</title>
<script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('.msg').click(function () {
var id = this.id;
$.ajax({
url: "/Category/Display",
data: { data: id },
success: function (mydata) {
$("#link").empty().append(mydata);
},
error: function (mydata) { alert("error"); },
type: "POST"
});
return false;
});
});
</script>
</head>
<body>
@foreach (var item in Model)
{
<a class="msg" href="#" id="@item.ProductId">@item.ProductName</a>
}
<div id="link">
</div>
</body>
</html>
ClicksUs.cshtml(部分视图)
@model List<PartialView.Models.tbl_product>
@foreach(var items in Model)
{
@items.ProductDesc
}
CategoryController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PartialView.Models;
namespace PartialView.Controllers
{
public class CategoryController : Controller
{
dbEntities dbentity = new dbEntities();
public ActionResult Create()
{
return View(dbentity.tbl_product.ToList());
}
public ActionResult Display(int data)
{
var query = dbentity.tbl_product.First(c => c.ProductId == data);
return PartialView("ClicksUC", query);
}
}
}
【问题讨论】:
-
你能发布你从 ajax 调用中得到了什么
-
Display 方法获取 id 假设如果我单击 id 为 2 的链接,则 Display 方法正在获取该 id 并查询检索 id 为 2 的项目的详细信息,但是当 ajax 中的控件函数进入错误块,如果我打印 alert(mydata) 它打印 [object Object] 请给出任何建议..
-
用json返回部分视图:return View("PartialViewName",jsonData)
-
在这一行
return PartialView("ClicksUC", query);部分视图的名称是ClicksUC。但是你之前说的名字是ClicksUs.cshtml。我认为是拼写错误。 -
它的 ClicksUC.cshtml 我认为 ajax 代码有任何错误,因为该值是从控制器方法正确返回的,但部分视图没有更新,当我单击时它不显示任何记录在链接上..
标签: c# jquery ajax asp.net-mvc asp.net-mvc-3