System.ArgumentNullException:'值不能为null错误

问题描述 投票:0回答:1

我对MVC还是很陌生,我正在尝试创建一种形式的上传文件输入类型,该格式允许用户上传文件,它读取内容并将其显示在表格中。但我不断收到此错误。出现此错误的行是我视图中的if Model.Count行,但不确定如何解决。

这是我的代码:

控制器:

      public ActionResult Index()
    {
        return View(new List<CategoryModel>());
    }

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase postedFile)
    {
        List<CategoryModel> categories = new List<CategoryModel>();
        string filePath = string.Empty;
        if (postedFile != null)
        {
            string path = Server.MapPath("~/Uploads/");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            filePath = path + Path.GetFileName(postedFile.FileName);
            string extension = Path.GetExtension(postedFile.FileName);
            postedFile.SaveAs(filePath);

            //Read the contents of CSV file.
            string csvData = System.IO.File.ReadAllText(filePath);


            //Execute a loop over the rows.
            foreach (string row in csvData.Split('\n'))
            {
                if (!string.IsNullOrEmpty(row))
                {
                    categories.Add(new CategoryModel
                    {
                        CategoryID = Convert.ToInt32(row.Split(',')[0]),
                        Name = row.Split(',')[1],
                        Item = row.Split(',')[2]
                    });
                }
            }
        }

        return View();
    }
}

}

和型号:

{
public class CategoryModel
{
    public int CategoryID { get; set; }
    public string Name { get; set; }   
    public string Item { get; set; }
}

并查看:

 @{
    ViewBag.Title = "View";
}

<h2>View</h2>

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <span>Select File:</span>
        <input type="file" name="postedFile" />
        <hr />
        <input type="submit" value="Upload" />
    }
    @if (Model.Count() > 0)
    {
        <hr />
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th>CategoryID</th>
                <th>Name</th>
                <th>Item</th>
            </tr>
            @foreach (CategoryModel category in Model)
            {
                <tr>
                    <td>@category.CategoryID</td>
                    <td>@category.Name</td>
                    <td>@category.Item</td>
                </tr>
            }
        </table>
    }
</body>
</html>
c# asp.net model-view-controller
1个回答
0
投票

当您在控制器操作中执行此操作时:

return View();

您没有将模型传递给视图,因此视图中的Modelnull。当您尝试在.Count()引用上调用扩展方法(例如null)时,这就是您得到的错误。 (因为从技术上讲,它不是NullReferenceException,而是扩展方法检查第一个参数是否为null并引发异常。)

您需要在视图中检查null,或始终传递模型的实例。就像您在第一个控制器操作中所做的一样:

return View(new List<CategoryModel>());

另外,在成功执行POST操作后,通常习惯上重定向用户而不是返回视图。这样,浏览器将发送一个新的HTTP GET请求进行重定向,因此,如果用户重新加载页面,它不会重新发布表单,而只是重新加载GET。

所以代替这个:

return View();

您可以这样做:

return RedirectToAction("Index");
© www.soinside.com 2019 - 2024. All rights reserved.