编辑: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方法中将整个数据从表单和图像一起保存到数据库(从实体类)?我正在寻找这样的例子一起工作好... –