Asp.net 从JQuery加载部分视图未在MVC中显示
使用JQuery加载部分视图时,在ASP.NETMVC1.0中渲染它时遇到问题 我有一个控制器,比如:Asp.net 从JQuery加载部分视图未在MVC中显示,asp.net,jquery,asp.net-mvc,partial-views,Asp.net,Jquery,Asp.net Mvc,Partial Views,使用JQuery加载部分视图时,在ASP.NETMVC1.0中渲染它时遇到问题 我有一个控制器,比如: public ActionResult Index() { return View(_invoiceService.FindAllInvoices()); } public ActionResult InvoiceSearchResults() { return PartialView("InvoiceSearchResults",_i
public ActionResult Index() {
return View(_invoiceService.FindAllInvoices());
}
public ActionResult InvoiceSearchResults() {
return PartialView("InvoiceSearchResults",_invoiceService.FindAllInvoices());
}
我有一个索引视图:
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.Script("/scripts/InvoiceSearch.js") %>
<h2>Search</h2>
<div class="SearchBar">
</div>
<div class="SearchResults"></div>
<p><%= Html.ActionLink("Create New", "Create") %></p>
</asp:Content>
我已经删除了一些代码,我知道这些代码是为了使问题更容易阅读
当我单击MyDropDownList时,它调用JQuery,转到我的控制器,似乎运行分部类,但它不呈现任何内容
我试过唐纳德的答案,同样的事情也发生了,也许我在什么地方做了一些愚蠢的事情
非常感谢您提供的任何帮助或一般建议。我有一个解决方案: 而不是使用
$("#SearchResults").load("/Invoice/InvoiceSearchResults/");
尝试使用$.ajax()请求控制器,然后将答复放入html中。我已经成功地完成了这项工作,我将尝试解释以下答案:
控制器方法
function doSubmitSuccess(result)
{
$('div#MyDiv').html(result);
}
保持原样
public ActionResult InvoiceSearchResults()
{
return PartialView("InvoiceSearchResults",_invoiceService.FindAllInvoices());
}
Ajax调用
$.ajax({
url: "/Invoice/InvoiceSearchResults/",
type: 'GET',
dataType: 'html', // <-- to expect an html response
success: doSubmitSuccess
});
应该是:
$(".SearchResults").load("/Invoice/InvoiceSearchResults/");
选择器问题-无法查看它以查看它
谢谢(我已经投票支持你的答案)嗯。。。虽然上面的代码应该可以工作(也可以使用$.ajax),但我一直在使用第三种方法来呈现我的片段。一个$get请求 这是一个样品
$.get(postUrl, function(data) {
$("#posts").append(data);
$('#ajaxLdMore').addClass('hideElement');
$('#ldMore').removeClass('hideElement');
});
所以,也许你会第三次幸运。对不起,这篇文章的格式已经改变:)。需要脱机,稍后将重新发布>查看Fiddler或Firebug网络面板中服务器的响应。它看起来正确吗?这是一个XMLHHTRequest,看起来还可以-我应该使用JSON还是其他东西,因为它不是html?你不需要使用JSON来做这件事。谢谢-同样的事情正在发生。我错过了一些基本的东西吗?这两个(load和ajax/success/html)做同样的事情。感谢您向我展示您可以通过load()方法调用控制器!直到现在,我才意识到你能做到:D
$("#SearchResults").load("/Invoice/InvoiceSearchResults/");
$(".SearchResults").load("/Invoice/InvoiceSearchResults/");
$.get(postUrl, function(data) {
$("#posts").append(data);
$('#ajaxLdMore').addClass('hideElement');
$('#ldMore').removeClass('hideElement');
});