0

我正在使用 ASP.NET MVC 并按照 ASP.NET 网站“getting-started-with-ef-using-mvc”上的教程说明进行操作。我有一个设备

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; }
    }

我的任务是,当用户添加新设备时,在创建表单上应该有一个文件上传控件,允许选择一个文件,当保存记录时,该文件应复制到文件夹中,其路径应存储在 FIELD"装备图像。

这是View的cshtml

@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>

最后但并非最不重要的设备控制器

 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);


    }
4

1 回答 1

1

确保将enctype表单的属性设置为“multipart/form-data”。

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

请参阅在 Razor 表单视图中包含文件上传

于 2013-02-12T10:35:03.427 回答