【问题标题】:MVC post complex HttpPostedFileBaseMVC 发布复杂的 HttpPostedFileBase
【发布时间】:2015-01-07 18:29:20
【问题描述】:

我想将复杂模型属性发布到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

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4


    【解决方案1】:
    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)
    

    【讨论】:

      【解决方案2】:

      检查视图并查看“encetype”

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-20
        • 2013-02-25
        • 1970-01-01
        • 2017-10-22
        • 1970-01-01
        相关资源
        最近更新 更多