我想使用ajax更新局部视图,但由于某种原因失败。为什么我的JQuery函数在尝试渲染局部视图时失败
控制器:
[HttpPost()]
public ActionResult DisplaySections(string id)
{
DataContext db = new DataContext();
var Data = (from p in db.vwData.Where(a => a.CourseId == id)
group p by p.SectionId into g
select g.Key).ToList();
return PartialView("DisplaySections", Data);
}
阿贾克斯:
$('#CourseId').focusout(function (e) {
e.preventDefault();
var link = '/Course/DisplaySections';
$.ajax({
type: 'POST',
url: link,
data: { id: $('#CourseId').val() },
dataType: 'html',
success: function (result) {
$("#partial").html(result);
},
error: function (result) {
alert("Failed")
}
});
});
部分:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%@ Import Namespace="Course.Models" %>
<table>
<% if (Model != null)
foreach (var item in Model) {
if (item == null) continue; %>
<tr>
<td>
<%: item.SectionId%>
</td>
<td>
<%: item.Description%>
</td>
</tr>
<% } %>
</table>
母版视图:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%@ Import Namespace="Course.Models" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Course - Sections
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div style="text-align: left; height: 202px;">
<table>
<tr>
<th>Course Id</th>
<td><input type="text" name="CourseId" id="CourseId"/></td>
</tr>
<tr>
<th>Course Name</th>
<td><input type="text" name="CourseName" id="CourseName"/></td>
</tr>
</table>
<div id="partial">
<% Html.RenderPartial("DisplaySections"); %>
</div>
</div>
</asp:Content>
它是如何失败? – MNGwinn 2012-07-10 21:30:47
**如何失败?打开Firebug控制台,看看是否有任何错误。这可能是一百万件事情。没有jQuery?服务器正在返回错误500?您发布的网址与您的期望不符? – 2012-07-10 21:31:07
@MNGwinn它只要执行控制器动作,我甚至可以看到数据,当我将鼠标悬停在控制器动作中的“数据”上,但是它将“错误”返回给jQuery函数。难道它控制器行动(列表)和jQuery(html)中的数据类型不匹配? – user793468 2012-07-10 21:46:23