问题描述
这是我的相册模型课。
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,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,new { @class = "form-control" })
<label> Genre </label>
@Html.TextBoxFor(m => m.Genre.Name,new { @class = "form-control" })
<label> Price </label>
@Html.TextBoxFor(m => m.Price,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",new
{
@class = "btn btn-info"
})
</div>
}
</div>
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;
}
}
每次我尝试更改相册的某些内容时,例如其标题或价格,在保存更改后我已经保存的图像都会丢失。如果有人能帮助我找出问题所在,我将不胜感激。预先感谢!
解决方法
我认为这一行中有error
。您没有从视图中正确传递albumId
参数。
<img class="img-thumbnail" width="150" height="150"
src="@Url.Action("GetImage","Album",new { Model.Id })" />
解决方案:
使用此方法将albumId
从view
传递到controller
<img class="img-thumbnail" width="150" height="150"
src="@Url.Action("GetImage",new { albumId = Model.Id })" />