文件未过帐到ActionResult.如何在MVC c#上上传文件

本文关键字:文件 MVC ActionResult | 更新日期: 2023-09-27 18:26:19

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

我的班级电影:

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

文件未过帐到ActionResult.如何在MVC c#上上传文件

通过查看代码,Action Result HttpPostedFileBase声明名称"files"和File upload控件名称不相同。

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

IEnumerable<HttpPostedFileBase> files

name="files"HttpPostedFileBase files必须相同。。。

保持相同的名称。。它会起作用的。,