2016-12-24 62 views
0

我想將圖像上傳到數據庫(ShoppingItems)或將圖像保存到我的項目中的文件夾,並將圖像的路徑插入數據庫。任何人都可以幫忙嗎?這是我的代碼(視圖):使用c將數據插入數據庫#

@using (Html.BeginForm("Index3", "Upload", FormMethod.Post)) 
{ 
    @Html.TextBoxFor(x => x.Itemname, new { @class = "form-control", placeholder = "Item name", required = "required" }) 
    <br /> 

    @Html.TextBoxFor(x => x.Price, new { @class = "form-control", placeholder = "Item price", required = "required" }) 
    <br /> 

    @Html.TextBoxFor(x => x.Quantity, new { @class = "form-control", placeholder = "Item quantity"}) 
    <br /> 

    @Html.TextBoxFor(x => x.AuthorIdentity, new { @class = "form-control", placeholder = "Username", required = "required" }) 
    <br /> 

    // THIS IS WHERE MY IMAGE UPLOAD SHOULD BE 
    <br /> 

    @Html.TextBoxFor(x => x.Category, new { @class = "form-control", placeholder = "Item category", required = "required" }) 
    <br /> 

    @Html.TextAreaFor(x => x.Description, new { @class = "form-control", placeholder = "Item description", required = "required" }) 
    <br /> 

    <input type="submit" class="btn btn-danger" value="Add" /> 
} 

控制器:

public ActionResult Index3(ShoppingItem formModel); 
{ 
    using (var ctx = new GikGamerModelDataContext()) 
    { 
     if (formModel == null) 
      return View(); 

     ctx.ShoppingItems.InsertOnSubmit(formModel); 
     ctx.SubmitChanges(); 
    } 

    return View(); 
} 

我上傳指數(INDEX3)只是顯示的文字,說您上載的是成功或失敗,所以我還沒有添加它: )

在順序形式

回答

0

上傳你必須指定屬性是enctype:與價值「的multipart/form-data的」 可以foloow的例子在此響應試試:Uploading/Displaying Images in MVC 4

希望它有幫助

+0

非常感謝。我完全忘了「multipart/form-data」部分:)聖誕快樂btw – GikGamer