I am currently using Steven Sanderson book Pro ASP.NET MVC 2 Framework. But I am using MVC 3 framwork. This question is related to my previous question. I am getting the following error...
Value cannot be null or empty. Parameter name: contentType pointing at line...
return File(product.Picture1, product.ImageMimeType);.In the book he says...
We don’t want either of these properties to be directly visible in the product-editing UI. We can use [ScaffoldColumn(false)] to exclude ImageMimeType from the automatically generated UI. We don’t need to give any hints about ImageData because ASP.NET MVC’s built-in object editor template won’t scaffold byte[] properties anyway—it only scaffolds properties of “simple” types like string, int, and DateTime.5.
So I have done that and still getting the same error message? If possible how could this be solved.
Index.cshtml
@foreach (var item in Model)
<td>
<img src="@Url.Action("GetImage", "ProductCategoryL2", new { id =
@item.SubProductCategoryID })" alt="" height="100" width="100" />
</td>
Edit.cshtml
@using (Html.BeginForm("ProductCategoryL2", "GetImage", FormMethod.Post,
new { @enctype = "multipart/form-data" }))
{
<div class="editor-field">
<img src="@Url.Action("GetImage", "ProductCategoryL2", new { id =
@Model.SubProd开发者_开发技巧uctCategoryID })" alt="" />
@Html.ValidationMessage("Picture1", "*")
<input type="file" name="Image" size="23"/>
</div>
}
cs file
public FileContentResult GetImage(int id)
{
var product = db.SubProductCategory2.First(x =>
x.SubProductCategoryID == id);
return File(product.Picture1, product.ImageMimeType); //Value cannot be null or empty. Parameter name: contentType
}
another cs file
[ScaffoldColumn(false)]
public string ImageMimeType { get; set; }
I have setup the field for ImageMimeType in the SQL Server 2008R2 exactly same was as in the book. I am using VS 2010 ASP.NET 4.0 MVC3. Thanks in advance. Table name is SubProductCategory2.
Put in a break point in the method and inspect the value of product.ImageMimeType. I think you'll find that it's either null or an empty string. Either it's not being retrieved from the database or the database doesn't contain anything in the column.
精彩评论