我不明白为什么我没有MVC AJAX

时间:2019-01-28 11:49:54

标签: c# html ajax asp.net-mvc asp.net-ajax

我有Get和Post部分操作。获取我在应用程序中拥有的图像列表。

[HttpGet]
public PartialViewResult ViewImageFileList()
{
    IEnumerable<string> allImages = Directory.EnumerateFiles(Server.MapPath("~/Images/NBAlogoImg/"));

    return PartialView(allImages);
}

发布我要删除的图片。

[HttpPost]
public PartialViewResult ViewImageFileList(string imageNameType)
{
    var fileToDeletePath = Path.Combine(Server.MapPath("~/Images/NBAlogoImg/"), imageNameType);

    if (System.IO.File.Exists(fileToDeletePath))
    {
        fileOperations.Delete(fileToDeletePath);
    }

    return PartialView();
}

我的部分视图的.chhtml

@model IEnumerable<string>

<div class="name-block-style">
     Логотипы которые имеются
</div>

<div id=team-logo-wrapper-images>
<ul>
    @foreach (var fullPath in Model)
    {
        var fileName = Path.GetFileName(fullPath);
        <li>
            <div class="box-name-image">

                <p class="image-name-type">@fileName</p>
                <img src="@Url.Content(string.Format("~/Images/NBAlogoImg/{0}", fileName))"
                     class="logo-images" alt="Логотип команды"
                     title="Логотип команды" />
            </div>
        </li>
    }
</ul>

<div id="delete-image-form" class="form-group">
     @using (Ajax.BeginForm(
    "ViewImageFileList",
    "Team",
     new AjaxOptions() { HttpMethod = "POST", OnComplete = "reloadPage()" }))
{
    <label>Введите имя с указание типа изображения</label>
    <input type="text" class="form-group" name="imageNameType" id="imageNameType" />
    <input type="submit" value="Удалить" class="btn btn-primary" />
}
</div>

<script>
    function reloadPage() {
        location.reload();
    }
</script>

我的问题是在编写删除的图像并提交时使用空引用(我通过ajax进行操作)。我遇到此错误Null reference,但是当我单击继续时,该图像被删除,我的脚本重新加载页面。

我想了解为什么要使用null以及如何解决它,因为删除图片时,它总是会停止我的应用程序。

2 个答案:

答案 0 :(得分:1)

问题在于,删除图像后进行POST时,您并不会像在ViewImageFileList中所做的那样填充局部视图的模型。当View Engine尝试构建您将在POST之后发送给客户端的视图时,如果尝试对空引用执行foreach时会得到空引用异常,则会产生这种结果。

话虽如此,您需要做的就是将所有图像传递给PartialView。因此,只需在您发布以下内容的操作方法中的return语句之前添加:

var allImages = Directory.EnumerateFiles(Server.MapPath("~/Images/NBAlogoImg/"));
return PatialView(allImages);

答案 1 :(得分:0)

浏览图像时,返回通过模型的视图

return PartialView(allImages); //allImages is a model

但是当您删除图像时,您将返回没有任何模型的视图

return PartialView(); //need to pass a model

因此,删除后,您想重定向到ViewImageFileList进行浏览 所有图片

[HttpPost]
public RedirectToRouteResult ViewImageFileList(string imageNameType)
{
    var fileToDeletePath = Path.Combine(Server.MapPath("~/Images/NBAlogoImg/"), imageNameType);

    if (System.IO.File.Exists(fileToDeletePath))
    {
        fileOperations.Delete(fileToDeletePath);
    }

    return RedirectToAction("ViewImageFileList");
}

或再次执行删除操作中的图像并将列表传递给视图

[HttpPost]
public PartialViewResult ViewImageFileList(string imageNameType)
{
    var fileToDeletePath = Path.Combine(Server.MapPath("~/Images/NBAlogoImg/"), imageNameType);

    if (System.IO.File.Exists(fileToDeletePath))
    {
        fileOperations.Delete(fileToDeletePath);
    }

    IEnumerable<string> allImages = Directory.EnumerateFiles(Server.MapPath("~/Images/NBAlogoImg/"));

    return PartialView(allImages);
}
相关问题