C# &引用;索引超出范围。必须为非负且小于集合的大小。\r\n参数名称:索引
我目前正在学习使用asp.NETMVC5进行web开发,现在正在从事我的学校项目。我想存储和显示数据库中的图像。我遇到了这个错误 这是我的代码这是控制器:C# &引用;索引超出范围。必须为非负且小于集合的大小。\r\n参数名称:索引,c#,asp.net-mvc,C#,Asp.net Mvc,我目前正在学习使用asp.NETMVC5进行web开发,现在正在从事我的学校项目。我想存储和显示数据库中的图像。我遇到了这个错误 这是我的代码这是控制器: public ActionResult AddItems() { return View(); } [HttpPost] public ActionResult AddItems(FormCollection form) { StoreItems i = new StoreItems(); //i.ID = int
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)
您是如何将文件从客户端发布到服务器的?此错误背后的问题是请求没有任何文件。@ChetanRanpariya我的想法正是-这可能应该添加为答案。您确定要将文件与表单一起发布吗。提供一个可用于重现您的问题的方法。
[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");
}