简体   繁体   English

ASP.NET MVC中的HttpPostedFileBase编辑器模板

[英]HttpPostedFileBase Editor Template in ASP.NET MVC

I am working with ASP.NET MVC 5 and have this simple ViewModel. 我正在使用ASP.NET MVC 5,并具有此简单的ViewModel。

public class ArtistVM
{
    public string FName { get; set; }
    public string LName { get; set; }
    public HttpPostedFileBase ImgUpload { get; set; }
}

I know that I require a custom editor template for HttpPostedFileBase , so I have gone ahead and added a HttpPostedFileBase.cshtml to ~/Views/Shared/EditorTemplates/ inside that I have the following: 我知道我需要HttpPostedFileBase的自定义编辑器模板,因此我将HttpPostedFileBase.cshtml添加到~/Views/Shared/EditorTemplates/ ,其中包含以下内容:

@model HttpPostedFileBase

@Html.TextBoxFor(model => model, new { type = "file" })

This seem to work OK as far as displaying the input of type file! 就显示类型文件的输入而言,这似乎可以正常工作!

图片

The problem occurs when it comes to posting and binding, my ImgUpload property always comes back as null, which tells me something is not right in my Editor Template. 问题是在发布和绑定时发生的,我的ImgUpload属性始终返回为null,这告诉我编辑器模板中的某些内容不正确。

Code for the new artist view is shown below: 新艺术家视图的代码如下所示:

@using (Html.BeginForm("New", "Artist", FormMethod.Post, new { encrypt = "multipart/form-data"})) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.FName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.FName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.FName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.LName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ImgUpload, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ImgUpload, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ImgUpload, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Add Artist" class="btn btn-primary" />
            </div>
        </div>
    </div>
}

And the Controller Action handling the post is: 处理该帖子的Controller Action是:

[HttpPost]
public ActionResult New(ArtistNewVM vm)
{
    var validImageTypes = new string[]
    {
        "image/gif",
        "image/jpeg",
        "image/pjpeg",
        "image/png"
    };

    if (vm.ImgUpload == null || vm.ImgUpload.ContentLength == 0)
    {
        // ** gets inside here **
        ModelState.AddModelError("ImageUpload", "This field is required");
    }
    else if (!validImageTypes.Any(vm.ImgUpload.ContentType.Contains))
    {
        ModelState.AddModelError("ImageUpload", "Please choose either a GIF, JPG or PNG image.");
    }

    if (ModelState.IsValid)
    {
        // do the magic
    }

    return View(vm);
}

You have html attribute wrong in form it is enctype which stands for "encoding type" not encrypt see here 您的html属性格式错误,它是代表“编码类型”的 enctype而不是encrypt 请参见此处

The enctype attribute's purpose is to indicate how form data should be encoded prior to it being sent to the location defined in the action attribute. enctype属性的用途是指示在将表单数据发送到action属性中定义的位置之前应如何编码。

Change: 更改:

new { encrypt = "multipart/form-data"}

to: 至:

new { enctype = "multipart/form-data"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM