0

I am using ASP.NET MVC and following the instructions on tutorial at ASP.NET website "getting-started-with-ef-using-mvc". I have an Equipment Class

public class Equipment
    {
       [Key] public int EquipID { get; set; }
        public string EquipName { get; set; }
        public string EquipDescription { get; set; }
        public string EquipSerialNumber { get; set; }
        public string EquipImage { get; set; }
        public string EquipManufacturor { get; set; }
        public string EquipLocation { get; set; }
        public string EquipCalibrationFreq { get; set; }
        public virtual ICollection<Calibration> Calibrations { get; set; }
    }

My task is that when user add a new Equipment, then on the Create form there should be a fileupload control which allows to select a file and when save the record this file should be copied in a folder and its path shall be stored in FIELD "EquipImage.

Here is the cshtml of View

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Equipment</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.EquipName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.EquipName)
            @Html.ValidationMessageFor(model => model.EquipName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.EquipDescription)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.EquipDescription)
            @Html.ValidationMessageFor(model => model.EquipDescription)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.EquipSerialNumber)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.EquipSerialNumber)
            @Html.ValidationMessageFor(model => model.EquipSerialNumber)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.EquipImage)
        </div>
        <input type="file" name="file"/>


        <div class="editor-label">
            @Html.LabelFor(model => model.EquipManufacturor)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.EquipManufacturor)
            @Html.ValidationMessageFor(model => model.EquipManufacturor)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.EquipLocation)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.EquipLocation)
            @Html.ValidationMessageFor(model => model.EquipLocation)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.EquipCalibrationFreq)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.EquipCalibrationFreq)
            @Html.ValidationMessageFor(model => model.EquipCalibrationFreq)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

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

and last but not least the Equipment Controller

 public ActionResult Create()
    {
        return View();
    } 

    //
    // POST: /Equipment/Create


    // This action handles the form POST and the upload
    [HttpPost]
    public ActionResult Create(HttpPostedFileBase file, Equipment equipment)
    {
        // Verify that the user selected a file
        if (file != null && file.ContentLength > 0)
        {
            // extract only the fielname
            var fileName = Path.GetFileName(file.FileName);
            // store the file inside ~/App_Data/uploads folder
            var path = Path.Combine(Server.MapPath("~/Content/EquipImages"), fileName);
            file.SaveAs(path);
        }       

        if (ModelState.IsValid)
              {
                db.Equipments.Add(equipment);
                db.SaveChanges();
                return RedirectToAction("Index");
              }
             return View(equipment);


    }
1
  • So, finally what is your problem ? Commented Feb 12, 2013 at 10:41

1 Answer 1

1

Make sure to set the enctype attribute of the form to "multipart/form-data".

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

See Including File Upload in Razor Form View

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

Comments

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.