从jQuery AJAX get操作返回视图

Returning view from jQuery AJAX get operation

本文关键字:返回 视图 操作 get jQuery AJAX      更新时间:2023-09-26

我正试图让一个异步jQuery访问一个MVC操作来返回一个视图。出于某种原因,尽管它访问操作,但不会渲染视图。

这是jQuery:

function showArchive(url) {
    var moreRecentThanDate = $("#chooseDate").val();
    $.get(url, { moreRecentThan: moreRecentThanDate });
}

这就是行动:

    [HttpGet]
    public ActionResult ShowArchive(DateTime moreRecentThan)
    {
        List<NewIndexViewModel> model = Service.GetArchives(moreRecentThan);
        ViewBag.IsArchive = true;
        return View("Index", model);
    }

PS:我从一个特殊的模式弹出窗口调用这个jQuery;

您应该使用如下语法将结果附加到某个位置:

$.get(url, { moreRecentThan: moreRecentThanDate }).done(function(data) {
    $('.result').html(data);
)};

如果有疑问,请查看jQuery文档,get(),我相信倒数第二个例子就是你想要的。。

$.get(url, { moreRecentThan: moreRecentThanDate })
.done(function(data) {
  $('#myElem').append(data);
  // or $('#myElem').html(data);
});
Jquery部件
$.get( url, { moreRecentThan: moreRecentThanDate }, function(data) {

//回调或响应$('#details_Div').replaceWith(data);

}); 

其中用户控制器具有一个名为details的动作,该动作执行:

public ActionResult Details( int id )
{
  //create a model
    return PartialView( "UserDetails", model );
}

PartialView在这里很重要。

这是假设您的局部视图是一个id为detailsDiv的容器,因此您只需将整个视图替换为调用结果的内容。

UserDetails部分视图http://evolpin.wordpress.com/2011/04/26/asp-net-mvc-partial-view-and-ajax-real-world-example/

<div id="details_Div">
    <HTML will be replaced once the ajax is completed>
</div>