在数据库中存储数据时,我得到红色的x标记而不是图片。我相信我在视图文件中有问题。请有人看看这个,并告诉我如何纠正它。如果我有错误的网址操作,请告诉我应该使用哪些网址。提前致谢。MVC3 - 从数据库中获取红色x而不是图片
SubCategory2表具有以下的列...
列字段>图片1:数据类型> VARBINARY(MAX)
列字段> ImageMimeType:数据类型> VARCHAR(50)
指数.cshtml文件
@foreach (var item in Model) {
<td>
<img src="@Url.Action("GetImage", "SubProductCategory2",
new { id = item.SubProductCategoryID})" alt="" height="100" width="100" />
</td>
Edit.cshtml文件 “编辑” 是在控制器上的方法。 “ProductCategoryL2”是控制器中的方法。 “GetImage”是控制器中的方法。所有这些方法都在叫ProductCategoryControllerL2
同一控制文件@using (Html.BeginForm("Edit", "ProductCategoryL2", "GetImage",
FormMethod.Post, new { @encType = "multipart/form-data" }))
{
<div class="editor-field">
<img src="@Url.Action("GetImage", "SubProductCategory2", new {
Model.SubProductCategoryID })" alt="" />
@Html.ValidationMessage("Picture1", "*")
<input type="file" id="fileUpload" name="Create" size="23"/>
</div>
}
ProductCategoryL2Controller.cs文件
[HttpPost]
public ActionResult Edit(int id, FormCollection collection,
SubProductCategory2 editSubProdCat, HttpPostedFileBase image)
{
var r = db.SubProductCategory2.First(x => x.SubProductCategoryID
== id);
if (TryUpdateModel(r))
{
if (image != null)
{
editSubProdCat.ImageMimeType = image.ContentType;
editSubProdCat.Picture1 = new byte[image.ContentLength];
image.InputStream.Read(editSubProdCat.Picture1, 0,
image.ContentLength);
}
db.SaveChanges();
return RedirectToAction("/");
}
return View(r);
}
public FileContentResult GetImage(int productId)
{
var product = db.SubProductCategory2.First(x =>
x.SubProductCategoryID == productId);
return File(product.Picture1, product.ImageMimeType);
}
增加注
我使用MVC 3框架。 GetImage方法已经从Steven Sanderson的书Pro ASP.NET MVC 2框架中提取出来。所以我不确定这是否会成为问题?
你是不是使用MVC3?你为它加了标签V2。 .also标记您使用的数据库。 – gideon 2011-03-18 17:25:13
什么是product.Picture1的类型? – 2011-03-18 17:30:17
@DiscoDude:另外,对于失败的情况,product.ImageMimeType的值是多少? – 2011-03-18 17:36:41