編輯:ASP.NET MVC上傳照片 - httpPostedFileBase爲null
照片在控制器POST方法空值,儘管我上傳它..
我的模型類:
class ProductVM{
public string Name { get; set;}
public string Color {get; set;}
public HttpPostedFileBase Photo1 { get; set; }
}
下面是我如何用剃刀實現視圖:
@model Project.Models.ProductVM
@using (Html.BeginForm("AddItem","Item", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new {@class = "text-danger"})
@Html.EditorFor(model => model.Name, new {htmlAttributes = new {@class"form-control"}})
@Html.ValidationMessageFor(model => model.Name)
// other fields editor's and dropdown's ...
<div class="col-xs-offset-2 col-xs-8 add-item-rectangle"><input type="file" name="@Model.Photo1" id="file"/></div>
<div class="col-xs-10 add-item-rectangle"></div>
<input type="submit" class="btn btn-block add-item-button-text" value="Send"/>
後控制器方法的:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddItem(ProductVM product)
{
//**heere when debuging Photo1 is null despite fact that i uploaded photo**
if (!ModelState.IsValid)
{
//... my stuffs
}
//..
return RedirectToAction("Index", "Home");
}
請參閱:[this](http://stackoverflow.com/questions/16255882/uploading-displaying-images-in-mvc-4) –
對不起,因爲我有點卡在那裏。我現在讀了大約5個例子。是否可以在用戶單擊我的矩形內並上傳照片後以DOM的形式顯示圖像,並且當單擊SUBMIT按鈕時從整個表單發送數據,然後在Post方法中將整個數據從表單和圖像一起保存到數據庫(從實體類)?我正在尋找這樣的例子一起工作好... –