-1

I cannot upload image in my MVC application. Does anyone know how I can fix this error?

Model Image:

namespace HelloWorld.Models
{
    public class Image
    {
        [Required]
        [DataType(DataType.MultilineText)]
        public string ImagePath { get; set; }

        public int ImageId { get; set; }
    }
}

Index view:

@model IEnumerable<HelloWorld.Models.Image>

@{
    ViewBag.Title = "Index";
}

<p>
    @Html.ActionLink("Create New", "Create")
</p>

@foreach (var item in Model) {
     <img src="@Html.DisplayFor(modelItem => item.ImagePath)">
}

Create view:

@model HelloWorld.Models.Image
@{
    ViewBag.Title = "Create";
}

 <form method="post" action="@Url.Action("Create", "Images")">
    <div>
        <h4>Image</h4>
        <hr />

        <input type="file" name="file">
        <input type="submit" value="Upload">
    </div>
</form>

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Create action:

[HttpPost]
        public ActionResult Create(Image img, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    file.SaveAs(HttpContext.Server.MapPath("../../Content/img/upload/") + file.FileName);
                    img.ImagePath = file.FileName;
                }
                db.Images.Add(img);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return RedirectToAction("Create","Images");
        }

I think my problem is that I have model state not valid. Because I have RedirectToAction("Create","Images");

6
  • 2
    what error do you get? Commented Jun 22, 2015 at 8:47
  • 2
    Set a breakpoint on your if and check the ModelState errors. Commented Jun 22, 2015 at 8:49
  • Model state not valid. I have redirect to Create action. Commented Jun 22, 2015 at 8:49
  • I think this is because Image object is always null Commented Jun 22, 2015 at 8:50
  • 1
    Add enctype="multipart/form-data" attribute to your form. Commented Jun 22, 2015 at 8:52

1 Answer 1

1

When you post files you need to include the multipart/form-data enctype on your form.

<form method="post" action="@Url.Action("Create", "Images")" enctype="multipart/form-data">
    <div>
        <h4>Image</h4>
        <hr />

        <input type="file" name="file">
        <input type="submit" value="Upload">
    </div>
</form>

Update

Create a view model for your file

public class FileViewModel
{
    [Required]
    public HttpPostedFileBase file { get; set; }
}

Then update your action as follows

    [HttpPost]
    public ActionResult Create(FileViewModel model)
    {
        if (ModelState.IsValid)
        {

            var image = new Image();

            model.SaveAs(HttpContext.Server.MapPath("../../Content/img/upload/") + model.FileName);
            image.ImagePath = model.FileName;

            db.Images.Add(image);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return RedirectToAction("Create", "Images");
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, but it's not working.
I just read your comments - Your ModelState is invalid because you arent passing any model data over. You ImagePath is required yet you aren't providing one. It looks like you dont even need the Image object as a parameter. Just initialise it in your action
@Geronimo I've updated my answer. I've removed the Image class from the action's parameter and intialised it inside the action. Furthermore, I have created a separate viewmodel for your file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.