1

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.SubProductCategoryID })" 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.

2
  • What is your db? What is the value of product.ImageMimeType? Commented Mar 19, 2011 at 20:19
  • That's your problem then. Make sure you have a value. Commented Mar 19, 2011 at 21:20

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

11 Comments

tvanfosson, did the step over and it says Value cannot be null or empty. Parameter name. I am thinking perhaps this code was for MVC2. When the new edition comes out in few months the code maybe difference. Any suggestions?
@Disco - did you look at the value of the parameter before stepping over it. I'm sure it's throwing the exception because the property is null. You need to figure out why the property doesn't have the value you expect. My best guess is that it isn't being stored in the database.
How shall I code it to store some value for ImageMimeType. The reason I ask because according the book there is nothing to say any value is being stored.
@Disco - You should be storing the mime type of the image, say image/jpeg, image/gif, image/png, whichever is correct, in the database when the image is uploaded. That way it knows which type to use when responding to the request for the image.
I attempted this - return File(product.Picture1, product.ImageMimeType, "image/jpg"); but this is wrong. So where do I put it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.