[英]Why is my JQuery function failing when trying to render partial view
I am trying to update a partial view using ajax, but for some reason its failing. 我正在尝试使用ajax更新局部视图,但由于某种原因其失败。
Controller: 控制器:
[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);
}
Ajax: 阿贾克斯:
$('#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")
}
});
});
Partial: 部分:
<%@ 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>
Master View: 主视图:
<%@ 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>
Your partial view is full of errors. 您的局部视图充满了错误。
Your controller action passes a List<string>
(or List<int>
depending on the type of the SectionId
property) to this view: 您的控制器操作将
List<string>
(或List<int>
取决于SectionId
属性的类型)传递给此视图:
[HttpPost()]
public ActionResult DisplaySections(string id)
{
DataContext db = new DataContext();
List<string> 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);
}
and yet in your partial view you are attempting to use some item.SectionId
and item.Description
. 但是在您的局部视图中,您尝试使用一些
item.SectionId
和item.Description
。
Start by making your view strongly typed so that you have Intellisense showing you what you can and cannot use at compile-time: 首先,使您的视图成为强类型,以便Intellisense向您显示在编译时可以使用和不能使用的内容:
<%@ Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<List<string>>"
%>
<table>
<% if (Model != null) { %>
<% foreach (string item in Model) { %>
<% if (item == null) continue; %>
<tr>
<td>
<%: item %>
</td>
</tr>
<% } %>
<% } %>
</table>
let's try a couple things: 让我们尝试一些事情:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.