2014-02-12 40 views
0

我想將複雜模型屬性發布到HttpPostedFileBaseMVC post complex 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

回答

0
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) 
0

選中查看,看看 「encetype」

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, 
new { enctype = "multipart/form-data", @class = "form-horizontal" }))