使用jQuery在MVC ASP.Net中加载部分视图

使用jQuery在MVC ASP.Net中加载部分视图,jquery,asp.net-mvc,Jquery,Asp.net Mvc,我对这两个都很陌生,所以请原谅我的基本问题 我有一个索引页,如: <div id="SearchBar"> <%= Html.Encode("Search by Site Name: ") + Html.TextBox("SearchTextBox") %> <input type="submit" value="Search" id="jQuerySubmit" /> </div> <

我对这两个都很陌生,所以请原谅我的基本问题

我有一个索引页,如:

<div id="SearchBar">              
    <%= Html.Encode("Search by Site Name: ") + Html.TextBox("SearchTextBox") %>    
    <input type="submit" value="Search" id="jQuerySubmit" />   
</div>
<div id="SiteList">   
    <% Html.RenderPartial("SiteIndexSearchResults", Model); %>                     
</div>
它进入控制器并基于IsAjaxResult为true返回新视图,但不刷新页面

提前谢谢

Davy

因为#jQuerySubmit是一个表单提交按钮,所以您需要防止正常提交表单的默认操作(没有AJAX)。要执行此操作,您需要从单击处理程序
返回false

$(document).ready(function() { 
    $("#jQuerySubmit").click(function() {
        $("#SiteList").load("/Site/IndexSearch/");         
        return false;
    });
})
由于#jQuerySubmit是一个表单提交按钮,因此您需要防止正常提交表单的默认操作(没有AJAX)。要执行此操作,您需要从单击处理程序
返回false

$(document).ready(function() { 
    $("#jQuerySubmit").click(function() {
        $("#SiteList").load("/Site/IndexSearch/");         
        return false;
    });
})
您需要停止对链接单击的“正常”处理。在大多数浏览器中,这是通过让单击处理程序返回
false
来实现的,但在Firefox中也可以使用,如下所示:

$(function() { // Shorthand for the $(document).ready(function() { - does the same
    $('#jQuerySubmit').click(function(ev) { // Takes the event as a parameter
        ev.preventDefault();
        $('#siteList').load('/Site/IndexSearch/');
        return false;
    });
});
$(function() {
    $('#jQuerySubmit').live('click', function(ev) {
        // The behavior is now applied to all links that are ever loaded on the page
        // that have the id set to jQuerySubmit.
        ...
    });
});
如果您希望将此行为应用于更多的AJAX链接,则可以使用
.live()
而不是
。单击()
,如下所示:

$(function() { // Shorthand for the $(document).ready(function() { - does the same
    $('#jQuerySubmit').click(function(ev) { // Takes the event as a parameter
        ev.preventDefault();
        $('#siteList').load('/Site/IndexSearch/');
        return false;
    });
});
$(function() {
    $('#jQuerySubmit').live('click', function(ev) {
        // The behavior is now applied to all links that are ever loaded on the page
        // that have the id set to jQuerySubmit.
        ...
    });
});
您需要停止对链接单击的“正常”处理。在大多数浏览器中,这是通过让单击处理程序返回
false
来实现的,但在Firefox中也可以使用,如下所示:

$(function() { // Shorthand for the $(document).ready(function() { - does the same
    $('#jQuerySubmit').click(function(ev) { // Takes the event as a parameter
        ev.preventDefault();
        $('#siteList').load('/Site/IndexSearch/');
        return false;
    });
});
$(function() {
    $('#jQuerySubmit').live('click', function(ev) {
        // The behavior is now applied to all links that are ever loaded on the page
        // that have the id set to jQuerySubmit.
        ...
    });
});
如果您希望将此行为应用于更多的AJAX链接,则可以使用
.live()
而不是
。单击()
,如下所示:

$(function() { // Shorthand for the $(document).ready(function() { - does the same
    $('#jQuerySubmit').click(function(ev) { // Takes the event as a parameter
        ev.preventDefault();
        $('#siteList').load('/Site/IndexSearch/');
        return false;
    });
});
$(function() {
    $('#jQuerySubmit').live('click', function(ev) {
        // The behavior is now applied to all links that are ever loaded on the page
        // that have the id set to jQuerySubmit.
        ...
    });
});

谢谢你的回答,两个都很有帮助。在我的控制器(驴子)中,我返回的是一个视图而不是一个PartialView

我现在有了一个文本框,它基于文本框val递增地将行返回到我的部分列表中,这是我一直想要的

再次感谢


戴维

谢谢你的回答——这两个答案都非常有用。在我的控制器(驴子)中,我返回的是一个视图而不是一个PartialView

我现在有了一个文本框,它基于文本框val递增地将行返回到我的部分列表中,这是我一直想要的

再次感谢


Davy

谢谢大家-仍然不起作用,并在IE和firefox中尝试了这两种方法。有什么想法吗?谢谢大家-仍然不起作用,并在IE和firefox中尝试了这两种方法。有什么想法吗?