2017-04-08 3 views
2

私は現在asp.net mvc5を使用してWeb開発を研究しています。 。。私は、このエラーが発生しましたデータベースから保存し、表示画像を希望"インデックスが範囲外です。コレクションのサイズよりも小さくなく、負ではありません。パラメータ名:インデックス

here is the screenshot

ここでは、これはコントローラである私のコードです:

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

} 
+0

クライアントからサーバーにファイルをどのようにポストしていますか? –

+2

このエラーの背後にある問題は、要求にファイルがないことです。 –

+0

@ChetanRanpariya私の考えはまさに - それはおそらく答えとして追加する必要があります。 – EJoshuaS

答えて

2

リクエストに投稿されたファイルはありません。つまり、配列は空です。したがって、インデックスの範囲外エラーです。また、防御コーディングを練習し、配列にデータが入力されていることを確認する必要があります。ファイルが必須の場合は正常にエラーが発生し、関連するエラーメッセージ(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"); 

} 
関連する問題

 関連する問題