我正在尝试使用 partialViews 创建示例 MVC4 网页
在我的父页面上,例如,Index.cshtml 页面我正在显示一个 partialView 页面,该页面将允许用户查看/更新个人资料照片
加载索引页面时,如果照片可用,我需要此部分页面显示照片
页面加载后,当用户上传新照片时,我只需要 partialView 页面进行 ajax 回发并显示新照片。
我可以使用从数据库中获取的照片加载页面,我可以通过单击“#btnPhotoUpload”按钮将新照片保存到数据库。 但是保存照片后,部分视图没有自动刷新。请帮助我如何让我的部分视图页面刷新并显示更新的照片。
这是我的索引页,即“Index.cshtml”
@model MvcSamples.Models.ViewModels.UserInfoViewModel
@{
ViewBag.Title = "Ajax Partial Postback demo";
ViewBag.UserId = 1;
}
<h2>PersonalInfo example</h2>
<div id="photoForm">
@Html.Partial("_UserPhoto")
</div>
<div id="OtherDetails">
@Html.Partial("_UserDetails")
</div>
这是我的PartialView,即_UserPhoto.cshtml
@model MvcSamples.Models.ViewModels.UserInfoViewModel
@using (Ajax.BeginForm("SaveProfilePhoto", "Example", new { id = "1" }, new AjaxOptions { UpdateTargetId = "photoForm", OnSuccess = "onSuccess" }, new { encType = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<a>
<img id="imgPhoto" width="100px" height="100px"/>
<label for="photo">Photo:</label>
<input type="file" name="photo" id="photo" />
<input id="btnPhotoUpload" type="button" value="Apply" />
</a>
<script type="text/javascript">
$(document).ready(function () {
$("#imgPhoto").attr('src', "@Url.Action("GetProfileImage", "Example", new { id = ViewBag.UserId })");
$("#btnPhotoUpload").click(function (event) {
//on-click code goes in here.
event.preventDefault();
SavePhotoToDb();
});
function SavePhotoToDb() {
var json;
var data;
$.ajax({
type: "POST",
url: "/Example/SaveProfilePhoto",
data: new FormData($("#form0").get(0)),
dataType: "html",
contentType: false,
processData: false,
success: saveItemCompleted(data),
error: saveItemFailed
});
}
function saveItemCompleted(data) {
$("#photoForm").html(data);
}
function saveItemFailed(request, status, error) {
}
});
</script>
}
这是我的控制器 ExampleController:
namespace MvcSamples.Controllers
{
public class ExampleController : Controller
{
IUserDetails usr = new UserDetails();
// GET: /Example/
[HttpGet]
public ActionResult Index()
{
//usr.GetProfilePhoto(WebSecurity.GetUserId(User.Identity.Name));
if (!string.IsNullOrWhiteSpace(User.Identity.Name))
{
ViewBag.UserId = WebSecurity.GetUserId(User.Identity.Name);
}
UserInfoViewModel model = new UserInfoViewModel();
model.GenderList = usr.FillGenderTypesDropDownList();
return View(model);
}
[HttpPost]
public ActionResult SaveProfilePhoto(HttpPostedFileBase photo, UserInfoViewModel model)
{
string path = @"C:\Temp\";
if (photo != null)
{
model.UserId = 1;//WebSecurity.GetUserId(User.Identity.Name);
ViewBag.UserId = model.UserId;
var binary = new byte[photo.ContentLength];
photo.InputStream.Read(binary, 0, photo.ContentLength);
UserPicModel upModel = new UserPicModel();
upModel.UserPhoto = binary;
upModel.UserId = model.UserId;
usr.InsertProfilePhoto(upModel);
}
return PartialView("_UserPhoto", model);
}
public FileResult GetProfileImage(int id)
{
byte[] barrImg = usr.GetProfilePhoto(id);
return File(barrImg, "image/png");
}
}
}
更新:
正如@David Tansey建议的那样,我在 SaveCompleted(data) 中添加了刷新图像的代码。
function RefreshImage() {
$("#imgPhoto").attr('src', function () {
// the datetime portion appended to the url avoids caching issues
// and ensures that a fresh image will be loaded every time
var d = new Date();
return this.src + '?' + d.getTime();
});
}
但是上面的代码只有在我点击两次上传按钮后才会刷新图像。实际上我需要这个来在$("#btnPhotoUpload").click
. 有什么建议么?
我还尝试在控制器上禁用缓存,但没有运气:
[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]