-1

如何从文件字段中获取数据?

我的班级电影:

public class Movie
{
    public int ID { get; set; }
    [Display(Name = "Movie Title")]
    [Required(ErrorMessage = "The Title Field Is Required.")]
    public string Title { get; set; }
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    [Display(Name = "Release Date")]
    public DateTime ReleaseDate { get; set; }
    [Required(ErrorMessage = "The Genere Field Is Required.")]
    public string Genre { get; set; }
    [DisplayFormat(DataFormatString = "{0:F3}")]
    public decimal Price { get; set; }
    public List<Image> Images = new List<Image>();
    public List<File> Files = new List<File>();
    public List<Link> Links = new List<Link>();

    public Movie()
    {
        ID = 0;
        Price = 0;
        Title = "movie";
        Genre = "דרמה";
        ReleaseDate = DateTime.Now;

        var image1 = new Image
        {
            ID = 0,
            FileName = ""
        };

        var image2 = new Image
        {
            ID = 0,
            FileName = ""
        };

        var image3 = new Image
        {
            ID = 0,
            FileName = ""
        };

        Images.Add(image1);
        Images.Add(image2);
        Images.Add(image3);
    }
}

我有一个图像编辑器模板:

@model BermanCRM.Models.Image
<div class="fl">
<h3>
    Image</h3>
<p>
    @Html.LabelFor(x => x.FileName)
    @Html.TextBoxFor(x => x.FileName, new { type = "file" })
</p>
<p>
    @Html.LabelFor(x => x.Order)
    @Html.EditorFor(x => x.Order)
</p>

</div>

我的电影创建视图:

@model BermanCRM.Models.Movie

@{
ViewBag.Title = "Create";
}

 <h2>Create</h2>

 @using (Html.BeginForm("Create", "Movies", FormMethod.Post, new { enctype = "multipart/form-data" }))
 {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>Movie</legend>

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

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

    <div class="editor-label">
        @Html.LabelFor(model => model.Genre)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.Genre, new SelectList(@ViewBag.Generelist, "Text", "Value"), "--select--")
        @Html.ValidationMessageFor(model => model.Genre)
    </div>

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

    <h2>Images</h2>
    @Html.EditorFor(model => model.Images)
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

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

我的控制器:

public ActionResult Create()
    {

        ViewBag.Generelist = listGeners;
        return View(new Movie());
    }

    //
    // POST: /Movies/Create

    [HttpPost]
    public ActionResult Create(Movie movie, IEnumerable<HttpPostedFileBase> files)
    {
        try
        {
            movie.Insert();

            foreach (HttpPostedFileBase file in files)
            {
                file.SaveAs(Path.Combine(Server.MapPath("~/Upload"), Path.GetFileName(file.FileName)));
            }

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }
4

1 回答 1

0

通过查看您的代码,操作结果HttpPostedFileBase声明名称“文件”和文件上传控件名称不同。

<input id="files" type="file" name="files" />


IEnumerable<HttpPostedFileBase> files

name="files"并且HttpPostedFileBase files必须相同...

保持相同的名字..它会工作.,

于 2013-08-05T05:59:19.447 回答