【问题标题】:I don't understand why I have null MVC AJAX我不明白为什么我有 null MVC AJAX
【发布时间】:2019-06-21 10:04:31
【问题描述】:

我有获取和发布部分操作。获取我在 ma 应用中的图像列表。

[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 以及如何修复它,因为它总是在我删除图像时停止我的应用程序。

【问题讨论】:

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


    【解决方案1】:

    当您浏览图像时,您会返回视图并传递模型

    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);
    }
    

    【讨论】:

    • return RedirectToAction("ViewImageFileList"); 不行,你的返回类型不完整
    • @remk93 是的,我的错。您只需将返回类型更新为ActionResultRedirectToRouteResult 即可使其正常工作。查看更新的答案。
    • 第二种情况更好,因为我有一个脚本来重新加载页面,而不是重定向到部分视图,因为在部分视图中你没有母版页
    【解决方案2】:

    问题在于,当您在删除图像后发布时,您不会填充局部视图的模型,正如您在 ViewImageFileList 中所做的那样。当视图引擎尝试构建您将在 POST 之后发送到客户端的视图时,这会导致在尝试对空引用执行 foreach 时获得空引用异常。

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

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

    【讨论】:

    • 谢谢你,我发现了我的愚蠢错误。但是谢谢你,做同样的动作!
    猜你喜欢
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    相关资源
    最近更新 更多