文件没有发布到的ActionResult。如何上传MVC的C#文件? [英] File is not posted to ActionResult. How to upload files on MVC c#?

查看:145
本文介绍了文件没有发布到的ActionResult。如何上传MVC的C#文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的电影类:

 公共类电影
{
    公众诠释ID {搞定;组; }
    [显示(名称=电影标题)]
    [必需的(=的ErrorMessage标题字段是必需的。)
    公共字符串名称{搞定;组; }
    [DisplayFormat(DataFormatString ={0:DD / MM / YYYY})]
    [显示(NAME =发布日期)]
    公众的DateTime RELEASEDATE {搞定;组; }
    [必需(的ErrorMessage =该Genere字段是必需的。)
    公共字符串类型{搞定;组; }
    [DisplayFormat(DataFormatString ={0:F3})]
    公共十进制价格{搞定;组; }
    公开名单<图像>照片=新的List<图像>();
    公开名单<文件>文件=新的List<文件>();
    公开名单< LINK>链接=新的List< LINK>();    大众电影()
    {
        ID = 0;
        价格= 0;
        标题=电影;
        类型=דרמה;
        RELEASEDATE = DateTime.Now;        VAR =此搜索新的图像
        {
            ID = 0,
            文件名=
        };        VAR IMAGE2 =新形象
        {
            ID = 0,
            文件名=
        };        VAR的Image3 =新形象
        {
            ID = 0,
            文件名=
        };        Images.Add(此搜索);
        Images.Add(IMAGE2);
        Images.Add(图像3);
    }
}

我有图像编辑模板:

  @model BermanCRM.Models.Image
< D​​IV CLASS =FL>
< H3>
    图片< / H3 GT&;
&所述p为H.;
    @ Html.LabelFor(X => x.FileName)
    @ Html.TextBoxFor(X => x.FileName,新的{type =文件})
&所述; / P>
&所述p为H.;
    @ Html.LabelFor(X => x.Order)
    @ Html.EditorFor(X => x.Order)
&所述; / P>< / DIV>

我的电影创造的看法:

  @model BermanCRM.Models.Movie@ {
ViewBag.Title =创建;
} < H2>创建< / H> @using(Html.BeginForm(创建,电影,FormMethod.Post,新{ENCTYPE =的multipart / form-data的}))
 {
@ Html.AntiForgeryToken()
@ Html.ValidationSummary(真)<&字段集GT;
    <传奇>电影< /传说>    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.Title)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.Title)
        @ Html.ValidationMessageFor(型号=> model.Title)
    < / DIV>    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.ReleaseDate)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.ReleaseDate)
        @ Html.ValidationMessageFor(型号=> model.ReleaseDate)
    < / DIV>    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.Genre)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.DropDownListFor(型号=> model.Genre,新的SelectList(@ ViewBag.Generelist,文本,值), - 选择 - )
        @ Html.ValidationMessageFor(型号=> model.Genre)
    < / DIV>    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.Price)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.Price)
        @ Html.ValidationMessageFor(型号=> model.Price)
    < / DIV>    < H2>图像和LT; / H>
    @ Html.EditorFor(型号=> model.Images)
    &所述p为H.;
        <输入类型=提交值=创建/>
    &所述; / P>
< /字段集>
}< D​​IV>
    @ Html.ActionLink(返回目录,索引)
< / DIV>

我的控制器:

 公众的ActionResult的Create()
    {        ViewBag.Generelist = listGeners;
        返回查看(新电影());
    }    //
    // POST:/电影/制作    [HttpPost]
    公众的ActionResult创建(动画电影的IEnumerable< HttpPostedFileBase>文件)
    {
        尝试
        {
            movie.Insert();            的foreach(HttpPostedFileBase文件中的文件)
            {
                file.SaveAs(Path.Combine(使用Server.Mappath(〜/上传),Path.GetFileName(file.FileName)));
            }            返回RedirectToAction(「指数」);
        }
        抓住
        {
            返回查看();
        }
    }


解决方案

通过看你的code中的操作结果 HttpPostedFileBase 声明名称的文件和文件上传控件的名称是不一样的。

 <输入ID =文件类型=文件名称=文件/>
IEnumerable的< HttpPostedFileBase>档

NAME =文件 HttpPostedFileBase文件必须是一样的...

请同名的..它会工作。

How to get the data from the file fields?

My class Movie:

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

I have an editor template for Image:

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

My movie create view:

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

My Controller:

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

解决方案

By looking your code the Action Result HttpPostedFileBase declaration name "files" and File upload control name is not same.

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


IEnumerable<HttpPostedFileBase> files

name="files" and HttpPostedFileBase files Must be same...

Keep the same name.. it will work.,

这篇关于文件没有发布到的ActionResult。如何上传MVC的C#文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆