c# - “索引超出范围。必须为非负数且小于集合的大小。\r\n参数名称 : index

标签 c# asp.net-mvc

我目前正在使用 ASP.NET MVC5 学习 Web 开发,现在正在从事我的学校项目。我想存储和显示数据库中的图像。我遇到了这个错误。

here is the screenshot

这是我的代码,这是 Controller :

public ActionResult AddItems()
{
    return View();
}

[HttpPost]
public ActionResult AddItems(FormCollection form)
{
    StoreItems i = new StoreItems();
    //i.ID = int.Parse(form["AlbumID"]);
    i.AlbumName = form["AlbumName"];
    i.Artist = form["AlbumArtist"];
    i.Genre = form["AlbumGenre"];
    i.DateReleased = DateTime.Parse(form["AlbumDateReleased"]);
    i.Price = int.Parse(form["AlbumPrice"]);
    i.Downloads = int.Parse(form["AlbumDownloads"]);
    i.Listens = int.Parse(form["AlbumListens"]);
    i.RecordLabel = form["RecordLabel"];
    HttpPostedFileBase file = Request.Files[0];
    i.PicturePath = file.FileName.ToString();

    DAL.AddItems(i);
    return RedirectToAction("ItemLists");
}

这是模型:

public static void AddItems(StoreItems i)
{

    byte[] bytes;
    if (string.IsNullOrEmpty(i.PicturePath))
    {
        string filename = System.Web.HttpContext.Current.Server.MapPath("~/Content/Images/default-artwork.png");
        bytes = System.IO.File.ReadAllBytes(filename);
    }

    else
    {
        string filename = i.PicturePath;
        bytes = System.IO.File.ReadAllBytes(filename);


    }


    SqlConnection con = new SqlConnection(DAL.cs);
    con.Open();
    SqlCommand com = new SqlCommand(
        "INSERT INTO AlbumsTb ( AlbumName, Artist, Genre, DateReleased, Price, Downloads, Listens, RecordLabel, DateAdded, AlbumArt) VALUES( @AlbumName, @Artist, @Genre, @DateReleased, @Price, @Downloads, @Listens, @RecordLabel, @DateAdded, @AlbumArt)", con);
    //com.Parameters.AddWithValue("@ID", i.ID);
    com.Parameters.AddWithValue("@AlbumName", SqlDbType.VarChar).Value = i.AlbumName;
    com.Parameters.AddWithValue("@Artist", SqlDbType.VarChar).Value = i.Artist;
    com.Parameters.AddWithValue("@Genre", SqlDbType.VarChar).Value = i.Genre;
    com.Parameters.AddWithValue("@DateReleased", SqlDbType.Date).Value = i.DateReleased;
    com.Parameters.AddWithValue("@Price",i.Price);
    com.Parameters.AddWithValue("@Downloads", i.Downloads);
    com.Parameters.AddWithValue("@Listens", i.Listens);
    com.Parameters.AddWithValue("@RecordLabel", SqlDbType.VarChar).Value = i.RecordLabel;
    com.Parameters.AddWithValue(@"DateAdded", DateTime.Now.ToString());

    com.Parameters.AddWithValue("@AlbumArt", SqlDbType.VarBinary).Value = bytes;

    com.ExecuteNonQuery();
    con.Close();

}

最佳答案

没有随请求发布的文件。这意味着数组为空。因此索引超出范围错误。您还应该练习防御性编码并检查以确保数组已填充。如果该文件是强制性的,那么您可以优雅地出错并返回相关错误消息(BadRequest..etc)

[HttpPost]
public ActionResult AddItems(FormCollection form)
{
    StoreItems i = new StoreItems();
    //i.ID = int.Parse(form["AlbumID"]);
    i.AlbumName = form["AlbumName"];
    i.Artist = form["AlbumArtist"];
    i.Genre = form["AlbumGenre"];
    i.DateReleased = DateTime.Parse(form["AlbumDateReleased"]);
    i.Price = int.Parse(form["AlbumPrice"]);
    i.Downloads = int.Parse(form["AlbumDownloads"]);
    i.Listens = int.Parse(form["AlbumListens"]);
    i.RecordLabel = form["RecordLabel"];
    var files = Request.Files;
    if(files.Count > 0) {
        var file = files[0];
        i.PicturePath = file.FileName.ToString();
    } else {
        //...return some error code or validation message.
    }

    DAL.AddItems(i);
    return RedirectToAction("ItemLists");

}

关于c# - “索引超出范围。必须为非负数且小于集合的大小。\r\n参数名称 : index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43295173/

相关文章:

c# - 登录按钮的 If 语句

c# - 我应该如何在我的业务/服务/应用程序层之间使用异步等待

asp.net-mvc - 处理 MVC 操作上的 WebDAV 请求

c# - 如何实现 Stack Overflow 的 "are you a human"特性?

c# - Windows 控制台应用程序语音不会改变

c# - 无法在我的解决方案中添加来自新项目的新引用

c# - 如何在单击游戏对象时创建菜单? (统一)

c# - 我可以将外部配置与 ASP.NET Core 的默认依赖注入(inject)一起使用吗?

asp.net - 如何在 ASP.NET 和 Windows 服务中使用相同的 DI 代码

c# - EF 迁移对象已存在错误