我刚刚开始使用 Html.RenderPartial(usercontrol, model) 来呈现我的用户控件。是否可以更改此功能以在部分视图加载时显示 Ajax 加载图像?
编辑:对此进行了尝试,但无法使其正常工作。我有这样的部分视图(_FixtureList.cshmtl):
@model List<Areas.Gameplan.Models.Fixture>
@foreach (var item in this.Model)
{
<tr>
<td class="teamgrid">@Html.Encode(item.Week)</td>
<td><img src='@Html.Encode(item.Logo)' alt="Logo" /></td>
<td class="teamgrid">@Html.Encode(item.Opponent)</td>
<td class="teamgrid">@Html.Encode(item.Result)</td>
</tr>
这就是我目前呈现页面的方式:
public ActionResult Cincinnati()
{
//renderpartial
List<Fixture> lstFixtures = _Base.DataRepository.GetFixtureList("2017", "Cincinnati");
return View(lstFixtures);
}
}
这是我观点的相关部分(Cincinnati.cshtml):
@model List<Areas.Gameplan.Models.Fixture>
@{
ViewBag.Title = "Cincinnati Bengals";
Layout = "~/Areas/Gameplan/Views/Shared/_Layout.cshtml";
}
<div id="bigborder">
<p>
<br />
<div class="sidebarleftteam">
<div id="divFixtures">
<table id='tblFixtures' align='center'><tr><th><img src='../../../../Content/Images/Gameplan/fixtureweek.jpg' /></th><th><img src='../../../../Content/Images/Gameplan/fixtureopponent.jpg' /></th><th/><th><img src='../../../../Content/Images/Gameplan/fixturescore.jpg' /></th></tr>
@{ Html.RenderPartial("_FixtureList", this.Model); }
</table>
我将如何将您的示例应用于此代码?
编辑 :
想通了,这就是我的做法:
public ActionResult MyPartial()
{
List<Fixture> lstFixtures = _Base.DataRepository.GetFixtureList("2016", "Cincinnati");
return PartialView("_FixtureList", lstFixtures);
}
在视图中:
$.ajax(
{
type: 'POST',
async: true,
contentType: 'application/json; charset=utf-8',
dataType: 'html',
url: 'MyPartial',
beforeSend: function (xhr) {
$('#mydiv').addClass('ajaxRefreshing');
xhr.setRequestHeader('X-Client', 'jQuery');
},
success: function (result) {
$('#mydiv').html("<table id='tblFixtures' align='center'><tr><th><img src='../../../../Content/Images/Gameplan/fixtureweek.jpg' /></th><th><img src='../../../../Content/Images/Gameplan/fixtureopponent.jpg' /></th><th/><th><img src='../../../../Content/Images/Gameplan/fixturescore.jpg' /></th></tr>" + result + "</table>");
},
error: function (error) {
alert(error);
},
complete: function () {
$('#mydiv').removeClass('ajaxRefreshing');
}
});