【问题标题】:HttpPostedFileBase is null every time I make changes asp.net mvc每次我进行更改 asp.net mvc 时,HttpPostedFileBase 都是空的
【发布时间】:2020-12-16 19:26:17
【问题描述】:

这是我的专辑模型类。

public class Album
    {
        [Required(ErrorMessage ="Please enter an album title.")]
        public string Title { get; set; }
        [Key]
        public int Id { get; set; }
        [Required(ErrorMessage ="Please enter an album price.")]
        [Range(0.01,double.MaxValue,ErrorMessage ="Price cannot be 0 or lower.")]
        public decimal Price { get; set; }
        [Required]
        public int ArtistId { get; set; }
        [Required]
        public virtual Artist Artist { get; set; }
        [Required]
        public int GenreId { get; set; }
        [Required]
        public virtual Genre Genre { get; set; }
        
        public byte[] ImageData { get; set; }
        public string ImageMimeType { get; set; }
    }

存储库和 SaveAlbum 方法的实现

public interface IAlbumRepository
    {
        IEnumerable<Album> Albums { get; }
        void SaveAlbum(Album album);
        Album DeleteAlbum(int albumId);
    }


public void SaveAlbum(Album album)
        {
            if (album.Id == 0)
            {
                if (context.Albums.Where(a => a.Artist.Name == album.Artist.Name).FirstOrDefault() != null)
                {
                    album.ArtistId = context.Albums.Where(a => a.Artist.Name == album.Artist.Name).FirstOrDefault().ArtistId;
                    album.Artist = context.Albums.Where(a => a.Artist.Name == album.Artist.Name).FirstOrDefault().Artist;
                }
                if (context.Albums.Where(a => a.Genre.Name == album.Genre.Name).FirstOrDefault() != null)
                {
                    album.GenreId = context.Albums.Where(a => a.Genre.Name == album.Genre.Name).FirstOrDefault().GenreId;
                    album.Genre = context.Albums.Where(a => a.Genre.Name == album.Genre.Name).FirstOrDefault().Genre;
                }
                context.Albums.Add(album);
            }
            else
            {
                Album dbEntry = context.Albums.Find(album.Id);
                if (dbEntry!= null)
                {
                    if (context.Albums.Where(a => a.Artist.Name == album.Artist.Name).FirstOrDefault() != null)
                    {
                        dbEntry.ArtistId = context.Albums.Where(a => a.Artist.Name == album.Artist.Name).FirstOrDefault().ArtistId;
                        dbEntry.Artist = context.Albums.Where(a => a.Artist.Name == album.Artist.Name).FirstOrDefault().Artist;
                    }
                    else
                    {
                        dbEntry.ArtistId = album.ArtistId;
                        dbEntry.Artist = album.Artist;
                    }
                    if (context.Albums.Where(a => a.Genre.Name == album.Genre.Name).FirstOrDefault() != null)
                    {
                        dbEntry.GenreId = context.Albums.Where(a => a.Genre.Name == album.Genre.Name).FirstOrDefault().GenreId;
                        dbEntry.Genre = context.Albums.Where(a => a.Genre.Name == album.Genre.Name).FirstOrDefault().Genre;
                    }
                    else
                    {
                        dbEntry.GenreId = album.GenreId;
                        dbEntry.Genre = album.Genre;
                    }
                    dbEntry.Id = album.Id;
                    dbEntry.ImageData = album.ImageData;
                    dbEntry.ImageMimeType = album.ImageMimeType;
                    dbEntry.OrderDetails = album.OrderDetails;
                    dbEntry.Price = album.Price;
                    dbEntry.Title = album.Title;
                }
            }
            context.SaveChanges();
        }

控制器

...
private IAlbumRepository repository;
        public AdminController(IAlbumRepository repo)
        {
            repository = repo;
        }
        public ViewResult Index()
        {
            return View(repository.Albums);
        }
        public ViewResult Edit(int albumId)
        {
            Album album = repository.Albums.FirstOrDefault(m => m.Id == albumId);
            return View(album);
        }
        [HttpPost]
        public ActionResult Edit(Album album, HttpPostedFileBase image = null)
        {
            if (ModelState.IsValid)
            {
                if (image != null)
                {
                    album.ImageMimeType = image.ContentType;
                    album.ImageData = new byte[image.ContentLength];
                    image.InputStream.Read(album.ImageData, 0, image.ContentLength);
                }

                repository.SaveAlbum(album);
                TempData["message"] = string.Format("{0} has been saved", album.Title);
                return RedirectToAction("Index");
            }
            else
            {
                return View(album);
            }
        }}

编辑视图

@model MusicStore1.Models.Album

@{
    ViewBag.Title = "Admit: Edit " + @Model.Title;
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<div>
    <div> <br /></div>
    <div>
        <h3> Edit @Model.Title </h3>
    </div>
    @using (Html.BeginForm("Edit", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.ValidationSummary()
        <div>
            @Html.HiddenFor(m => m.Id)
            <label> Title </label>
            @Html.TextBoxFor(m => m.Title, null, new { @class = "form-control" })
            <label> Artist </label>
            @Html.TextBoxFor(m => m.Artist.Name, null, new { @class = "form-control" })
            <label> Genre </label>
             @Html.TextBoxFor(m => m.Genre.Name, null, new { @class = "form-control" })
            <label> Price  </label>
            @Html.TextBoxFor(m => m.Price, null, new { @class = "form-control" })
            <div class="form-group">
                <div style="position:relative;">
                    <label> Image </label>
                    <a class="btn" href="javascript:;">
                        <input type="file" name="Image" size="40"
                               style="position:absolute" ;z-index:2;top:0;left:0;filter:alpha(opacity=0);opacity:0;
                               background-color:transparent; color:transparent;"
                               onchange='$("#upload-file-info").html($(this).val());' />
                    </a>
                    <span class="label label-info" id="upload-file-info"></span>
                </div>
                @if (Model.ImageData == null)
                {
                    <div class="form-control-static"> No Image</div>
                }
                else
                {
                    <img class="img-thumbnail" width="150" height="150" src="@Url.Action("GetImage","Album", new { Model.Id })" />
                }
            </div>
        </div>
        <div>
            <input type="submit" value="save" class="btn btn-success" />
            @Html.ActionLink("Cancel and return to List", "Index", null, new
       {
           @class = "btn btn-info"
       })
        </div>
    }
</div>

GetImage 方法

 public FileContentResult GetImage(int albumId)
        {
            Album alb = repository.Albums.FirstOrDefault(p => p.Id == albumId);
            if (alb != null)
            {
                return File(alb.ImageData, alb.ImageMimeType);
            }
            else
            {
                return null;
            }
        }

每次我尝试更改相册的某些内容时,例如它的标题或价格,我已经保存的图像会在我保存更改后丢失。如果有人可以帮助我并找出问题所在,我将不胜感激。提前致谢!

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-5


    【解决方案1】:

    我认为error 在这一行。您没有从视图中正确传递 albumId 参数。

    <img class="img-thumbnail" width="150" height="150"
        src="@Url.Action("GetImage","Album", new { Model.Id })" />
    

    分辨率: 使用它来将albumIdview 传递到controller

    <img class="img-thumbnail" width="150" height="150"
        src="@Url.Action("GetImage","Album", new { albumId = Model.Id })" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-22
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 2020-10-02
      • 2022-11-22
      • 2017-10-22
      • 2020-06-10
      相关资源
      最近更新 更多