如何使用jquery渲染局部视图?
我们可以像这样渲染局部视图:
<% Html.RenderPartial("UserDetails"); %>
我们如何使用jquery做同样的事情?
您不能仅使用jQuery渲染局部视图。但是,您可以调用一个方法(操作)来为您呈现局部视图,并使用jQuery / AJAX将其添加到页面中。在下面,我们有一个按钮单击处理程序,它从按钮上的数据属性加载操作的url,并触发GET请求,用更新的内容替换部分视图中包含的DIV。
$('.js-reload-details').on('click', function(evt) {
evt.preventDefault();
evt.stopPropagation();
var $detailDiv = $('#detailsDiv'),
url = $(this).data('url');
$.get(url, function(data) {
$detailDiv.replaceWith(data);
});
});
用户控制器具有名为详细信息的操作:
public ActionResult Details( int id )
{
var model = ...get user from db using id...
return PartialView( "UserDetails", model );
}
这假设您的局部视图是一个id为detailsDiv
的容器,因此您只需将整个事件替换为调用结果的内容即可。
父视图按钮
<button data-url='@Url.Action("details","user", new { id = Model.ID } )'
class="js-reload-details">Reload</button>
User
是控制器名称,details
是@Url.Action()
中的行动名称。 UserDetails局部视图
<div id="detailsDiv">
<!-- ...content... -->
</div>
我使用ajax加载来执行此操作:
$('#user_content').load('@Url.Action("UserDetails","User")');
@tvanfosson摇滚着他的回答。
但是,我建议在js内进行改进并进行小型控制器检查。
当我们使用@Url
帮助器调用一个动作时,我们将收到一个格式化的html。更新内容(.html
)而不是实际元素(.replaceWith
)会更好。
更多关于:What's the difference between jQuery's replaceWith() and html()?
$.get( '@Url.Action("details","user", new { id = Model.ID } )', function(data) {
$('#detailsDiv').html(data);
});
这在树中特别有用,其中内容可以多次更改。
在控制器上,我们可以根据请求者重用该操作:
public ActionResult Details( int id )
{
var model = GetFooModel();
if (Request.IsAjaxRequest())
{
return PartialView( "UserDetails", model );
}
return View(model);
}
您可以尝试的另一件事(基于tvanfosson的答案)是这样的:
<div class="renderaction fade-in"
data-actionurl="@Url.Action("details","user", new { id = Model.ID } )"></div>
然后在页面的脚本部分:
<script type="text/javascript">
$(function () {
$(".renderaction").each(function (i, n) {
var $n = $(n),
url = $n.attr('data-actionurl'),
$this = $(this);
$.get(url, function (data) {
$this.html(data);
});
});
});
</script>
这将使用ajax呈现您的@ Html.RenderAction。
为了让所有迷人的sjmansy你可以使用这个css添加淡入效果:
/* make keyframes that tell the start state and the end state of our object */
@-webkit-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@-moz-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
.fade-in {
opacity: 0; /* make things invisible upon start */
-webkit-animation: fadeIn ease-in 1; /* call our keyframe named fadeIn, use animattion ease-in and repeat it only 1 time */
-moz-animation: fadeIn ease-in 1;
-o-animation: fadeIn ease-in 1;
animation: fadeIn ease-in 1;
-webkit-animation-fill-mode: forwards; /* this makes sure that after animation is done we remain at the last keyframe value (opacity: 1)*/
-o-animation-fill-mode: forwards;
animation-fill-mode: forwards;
-webkit-animation-duration: 1s;
-moz-animation-duration: 1s;
-o-animation-duration: 1s;
animation-duration: 1s;
}
男人我喜欢mvc :-)
您需要在Controller上创建一个Action,它返回“UserDetails”局部视图或控件的渲染结果。然后只需使用来自jQuery的Http Get或Post来调用Action来显示渲染的html。
使用标准的Ajax调用来实现相同的结果
$.ajax({
url: '@Url.Action("_SearchStudents")?NationalId=' + $('#NationalId').val(),
type: 'GET',
error: function (xhr) {
alert('Error: ' + xhr.statusText);
},
success: function (result) {
$('#divSearchResult').html(result);
}
});
public ActionResult _SearchStudents(string NationalId)
{
//.......
return PartialView("_SearchStudents", model);
}
我是这样做的。
$(document).ready(function(){
$("#yourid").click(function(){
$(this).load('@Url.Action("Details")');
});
});
细节方法:
public IActionResult Details()
{
return PartialView("Your Partial View");
}