关于C#:用MVC形式上传

upload with mvc form

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

我的控制器动作开始:

1
2
public ActionResult Add(ArticleModel model, HttpPostedFileBase file)
...

我的表格:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<section id="">
@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()
    @ViewBag.Status


    <fieldset>
        <legend>Add</legend>
       
           
<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>

       
        <input type="submit" value="Add" />
    </fieldset>
}
</section>

编辑:

我的模特

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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; }
}

I have a simple question. Why in code below, this part - HttpPostedFileBase file - is null?

因为您没有在表单上将enctype设置为multipart/form-data

1
2
3
4
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype ="multipart/form-data" }))
{
    ...
}

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

在将Html.BeginForm切换为Ajax.BeginForm并问为什么HttpPostedFileBase file - is null之前,答案是您无法使用AJAX上传文件。 您将必须使用文件上传插件,例如Uploadify或Blueimp File upload。 我只是在记录中提及此内容,以避免进一步的重复问题。

更新:

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

1
2
3
4
5
6
7
public class ArticleModel
{
    [Required]
    public HttpPostedFileBase File { get; set; }

    ...
}

然后让您的控制器操作直接将此视图模型用作参数并摆脱HttpPostedFileBase参数:

1
2
3
4
5
6
7
public ActionResult Add(ArticleModel model)
{
    if (ModelState.IsValid)
    {
        ... work with model.File directly here
    }
}

另外,为了避免您的域模型和视图模型之间有任何歧义,我将在它们后面加上ViewModel:

1
2
3
4
public class ArticleViewModel
{
    ...
}