渲染用ajax管窥 [英] Rendering Partial Views using ajax
问题描述
我检查<一href="http://stackoverflow.com/questions/4888521/asp-net-mvc3-razor-views-and-partialviews-with-ajax-postbacks">this问题它解决了我最初的问题。但我不希望只有当用户点击一个链接的局部视图中呈现,我想呈现局部视图时,页面加载,可能的话,显示进度指示器正在加载的局部视图,而
I've checked this question and it solved my initial problems. But I don't want the partial view to be rendered only when the user clicks a link, I want to render partial views when the page is loaded and, possibly, show a progress indicator while the partial view is being loaded.
如何实现这一目标?
非常感谢你阅读这一点。
Thank you very much for reading this.
推荐答案
如果您要加载的页面,然后加载通过AJAX的局部视图,你可以创建一个 ActionMethod
这的确是这样的:
If you want to load the page and then load the partial view via ajax you could create an ActionMethod
that does something like:
public ActionResult Create()
{
var model = new MyModel();
if (Request.IsAjaxRequest())
{
return PartialView( "_Partial", model.PartialModel );
}
else
{
return View( model );
}
}
,然后在你的页面做这样的事情:
and then in your page do something like:
$(function(){
$(document).ajaxStart(function () {
//show a progress modal of your choosing
showProgressModal('loading');
});
$(document).ajaxStop(function () {
//hide it
hideProgressModal();
});
$.ajax({
url: '/controller/create',
dataType: 'html',
success: function(data) {
$('#myPartialContainer').html(data);
}
});
});
这篇关于渲染用ajax管窥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!