MVC3查看更新问题

Nav*_*hat 0 image modelstate asp.net-mvc-3

这是一个设置图像以ViewBag供显示的动作.

public ActionResult UploadPhoto()
{
    List<string> images = new List<string>();

    foreach (var file in Directory.GetFiles(AlbumManager.GetAlbumName(SessionManager.CurrentSession.PropertyId, true)))
    {
        images.Add(Server.RelativePath(file));
    }

    ViewBag.Images = images;

    return PartialView("UploadPhoto");
}
Run Code Online (Sandbox Code Playgroud)

这是一个用于删除所选图像的帖子操作.

[HttpPost]
public ActionResult DeletePhoto(string imageName)
{
    AlbumManager.DeletePhoto(SessionManager.CurrentSession.PropertyId, imageName);
    return UploadPhoto();
}
Run Code Online (Sandbox Code Playgroud)

如您所见,删除完成后,我将其重定向到UploadPhoto必须打印当前现有图像的操作.但在回发后,删除的图像仍然显示.我对此行为非常不确定.请帮我解决这个问题.

我尝试DeletePhoto使用以下代码清除模型状态,但没有用.

ModelState.Clear();
Run Code Online (Sandbox Code Playgroud)

我的看法:

@using (Html.BeginForm("DeletePhoto", "Add", FormMethod.Post, new { id = "PhotoDelete" }))
{
    <input type="hidden" name="imageName" id="imageName" />

    <div class="thumnailsBox">
        @foreach (var image in ViewBag.Images)
        {
            <div class="thumnailTails">
                <span class="thumbimage">
                    <input id="imageSubmit" type="image" alt="No Image" src="@Url.Content(image)" /></span>
            </div>
        }
    </div>
}
Run Code Online (Sandbox Code Playgroud)

解:

除了IronMan84的回答,这里是我问题的实际解决方案:

[HttpPost]
public ActionResult DeletePhoto(string imageName)
{
    AlbumManager.DeletePhoto(SessionManager.CurrentSession.PropertyId, imageName);
    return JavaScript("location.reload(true)");
}
Run Code Online (Sandbox Code Playgroud)

我正在渲染一个脚本来重新加载页面.

Jak*_*cki 5

你没有重定向到UploadPhoto动作.

使用RedirectToAction("UploadPhoto");重定向.

此外,您可以尝试调试代码以检查您AlbumManager是否未缓存数据或不执行删除.