我想将复杂模型属性发布到HttpPostedFileBase。
控制器
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(AlbumImagesLoc entity, int albumId, HttpPostedFileBase ImageUrl)
{ }视图
@Html.TextBoxFor(model => model.AlbumImage.ImageUrl, new { type = "file" })
@Html.ValidationMessageFor(model => model.AlbumImage.ImageUrl)现在的问题是HttpPostedFileBase总是null,因为它的名称必须是AlbumImage.ImageUrl。
如何避免MVC代码中的null?
发布于 2014-02-12 09:28:33
public class AlbumImagesLoc
{
public int Id { get; set; }
public string Name { get; set; }
public AlbumImage ImageUrl { get; set; }
}
[HttpGet]
public ActionResult Create()
{
return View(new AlbumImagesLoc());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(AlbumImagesLoc entity, int albumId)
{
var albumImagesLoc = new AlbumImagesLoc();
albumImagesLoc.Name = clientViewModel.Name;
albumImagesLoc.AlbumImage.Add(AlbumImagesLoc.ImageUrl);
db.AlbumImagesLoc.Add(albumImagesLoc);
db.SaveChanges();
return RedirectToAction("Index");
}
@Html.TextBoxFor(model => model.AlbumImage.ImageUrl, new { type = "file" })
@Html.ValidationMessageFor(model => model.AlbumImage.ImageUrl)发布于 2015-01-07 10:29:20
查看并查看"encetype“
@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post,
new { enctype = "multipart/form-data", @class = "form-horizontal" }))https://stackoverflow.com/questions/21723192
复制相似问题