Majoren
Majoren

Reputation: 1043

.NET MVC3 Image Upload related error: conversion from type 'System.Web.HttpPostedFileWrapper' to type 'Project.Domain.Entities.Image'

I will try to explain it simple:

I have an entity - Image - which looks like this:

public class Image : IEntity
    {
        public int ID
        {
            get;
            set;
        }
        public string name
        {
            get;
            set;
        }
        public virtual ICollection<Restaurant> Restaurant
        {
            get;
            set;
        }
    }
}

Here is the relevant properties in the Restaurant entity class:

[HiddenInput(DisplayValue = false)]
public Guid ImageID { get; set; }
public string ImageName { get; set; }
public virtual Image Image
        {
            get;
            set;
        }
        
        public byte[] ImageData 
        { 
            get; set; 
        }
        [HiddenInput(DisplayValue = false)]
        public string ImageMimeType 
        { 
            get; set; 
        }

And in my View, where the user takes an image and uploads it to save it for their user, I have a simple <input type="file" name="image" />, with an enctype = "multipart/form-data". The user in this case is an entity - Restaurant.

@using (Html.BeginForm("Edit", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))

[...]

<input type="file" name="image" />

[...]

Here is the ActionResult in AdminController, which should take care of the posted image (and other data from the html from).

[HttpPost]
        public ActionResult Edit(Restaurant rest, Address address, HttpPostedFileWrapper image)
        {
            if (ModelState.IsValid)
            {
                if (image != null && image.ContentLength > 0)
                {
                    var filename = Path.GetFileName(image.FileName);
                    Guid imageID = Guid.NewGuid();
                    var relativePath = @"~/Content/Images/Logotypes/" + imageID.ToString();
                    image.SaveAs(Server.MapPath(relativePath));

                    rest.ImageMimeType = image.ContentType;
                    rest.ImageName = filename;
                    rest.ImageID = imageID;

                    repo.Save(rest);
                }
                return View(rest);
            }
            else
            {
                var errors = ModelState
                .Where(x => x.Value.Errors.Count > 0)
                .Select(x => new { x.Key, x.Value.Errors })
                .ToArray();

                return View(rest);
            }
        }

When it runs the code, ModelState.IsValid() returns false, and the ModelState-error is as follows:

{System.InvalidOperationException: The parameter conversion from type 'System.Web.HttpPostedFileWrapper' to type 'Projct.Domain.Entities.Image' failed because no type converter can convert between these types.

Any Ideas?

Upvotes: 2

Views: 1400

Answers (1)

Jan
Jan

Reputation: 16042

The modelbinder tries to bind <input type="file" element which is named image to the Image property of your Restaurant class.

Just give the second parameter of your action and the <input type="file element another name than image.

Upvotes: 8

Related Questions