使用mvc表单上传

who*_*oah 1 c# asp.net asp.net-mvc asp.net-mvc-4

我有一个简单的问题.为什么在下面的代码中,这部分HttpPostedFileBase file- 是空的?当然ArticleModel model不是空的,只是file.

开始我的控制器动作:

public ActionResult Add(ArticleModel model, HttpPostedFileBase file)
...
Run Code Online (Sandbox Code Playgroud)

我的表格:

<section id="">
@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()
    @ViewBag.Status


    <fieldset>
        <legend>Add</legend>
        <ol>
            <li>
                @Html.LabelFor(m => m.title)
                @Html.TextBoxFor(m => m.title)
            </li>
            <li>
                @Html.LabelFor(m => m.Categories)
                @Html.DropDownListFor(m=> m.categoryID, Model.Categories, "- lorem -", new { @class="dropdownlist" })
            </li>
            <li>
                @Html.LabelFor(m => m.connectedArticlesID)
                @Html.TextBoxFor(m => m.connectedArticlesID)
            </li>
            <li>
                 <input type="file" name="file" id="file" />
            </li>
            <li>
                @Html.LabelFor(m => m.introduction)
                @Html.EditorFor(m => m.introduction)
            </li>
            <li>
                @Html.LabelFor(m => m.content)
                @Html.EditorFor(m => m.content)
            </li>
        </ol>
        <input type="submit" value="Add" />
    </fieldset>
}
</section>
Run Code Online (Sandbox Code Playgroud)

编辑:

我的模特

public class ArticleModel
{
    [Display(Name = "Number")]
    public int articleID { get; set; }

    [Required]
    [Display(Name = "Tilte")]
    [StringLength(250, ErrorMessage = "Tytu? musi mie? d?ugo?? od {0} do {2} znaków.", MinimumLength = 6)]
    public string title { get; set; }

    [Required]
    [Display(Name = "Similar articles")]
    public string connectedArticlesID { get; set; }

    [Display(Name = "CategoryName")]
    public string category { get; set; }

    [Required]
    [Display(Name = "Category")]
    public int categoryID { get; set; }

    [Required]
    [Display(Name = "Category")]
    public IEnumerable<SelectListItem> Categories
    {
        get
        {
            return new[]
            {
                new SelectListItem { Value = "1", Text = "Kategoria pierwsza" },
                new SelectListItem { Value = "2", Text = "Kategoria druga" },
                new SelectListItem { Value = "3", Text = "Kategoria trzecia" },
            };
        }
    }

    [Display(Name = "Content")]
    [Required]
    [StringLength(6000, ErrorMessage = "Tre?? musi mie? d?ugo?? od {1} do {2} znaków.", MinimumLength = 30)]
    [UIHint("tinymce_jquery_full"), AllowHtml]
    public string content { get; set; }

    [Display(Name = "Introduction")]
    [Required]
    [StringLength(6000, ErrorMessage = "Wst?p musi mie? d?ugo?? od {1} do {2} znaków.", MinimumLength = 30)]
    [DataType(DataType.MultilineText)]
    public string introduction { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 6

我有一个简单的问题.为什么在下面的代码中,这部分 - HttpPostedFileBase文件 - 是空的?

因为你没有设置enctypemultipart/form-data您的形式:

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" })) 
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

参考:http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx

在你切换Html.BeginForm到a Ajax.BeginForm然后问一个问题为什么之前 HttpPostedFileBase file - is null,答案是你无法使用AJAX上传文件.您将不得不使用文件上传插件,如Uploadify或Blueimp文件上传.我只是提到这个记录,以避免进一步可能的重复问题.


更新:

根据评论部分的要求,您可以将文件字段添加到视图模型中:

public class ArticleModel
{
    [Required]
    public HttpPostedFileBase File { get; set; }

    ...
}
Run Code Online (Sandbox Code Playgroud)

然后让你的控制器动作直接将此视图模型作为参数并删除HttpPostedFileBase参数:

public ActionResult Add(ArticleModel model)
{
    if (ModelState.IsValid)
    {
        ... work with model.File directly here
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,为了避免域模型和视图模型之间存在任何歧义,我将使用ViewModel为它们添加后缀:

public class ArticleViewModel
{
    ...
}
Run Code Online (Sandbox Code Playgroud)