asp.net – 使用“DataType.Upload”属性为属性生成文件上传输入?

前端之家收集整理的这篇文章主要介绍了asp.net – 使用“DataType.Upload”属性为属性生成文件上传输入?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下视图模型.
public class Myviewmodel
{
    [DataType(DataType.Upload)]
    public HttpPostedFileBase ImageUpload { get; set; }

    public int VenueId { get; set; }
    public virtual Venue Venue { get; set; }
    .... // other properties
}

我正在按照此页面http://cpratt.co/file-uploads-in-asp-net-mvc-with-view-models/创建图像上传控件.这是视图代码.

@using (Html.BeginForm("Create","Event",FormMethod.Post,new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

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

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

但是,它生成三个文本框而不是文件上传输入控件?以下HTML代码.

<div class="col-md-10">
    <div class="editor-label"><label for="ImageUpload_ContentLength">ContentLength</label></div>
    <div class="editor-field"><input name="ImageUpload.ContentLength" class="text-Box single-line" id="ImageUpload_ContentLength" type="number" value="" data-val-required="The ContentLength field is required." data-val-number="The field ContentLength must be a number." data-val="true"> <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="ImageUpload.ContentLength"></span></div>
    <div class="editor-label"><label for="ImageUpload_ContentType">ContentType</label></div>
    <div class="editor-field"><input name="ImageUpload.ContentType" class="text-Box single-line" id="ImageUpload_ContentType" type="text" value=""> <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="ImageUpload.ContentType"></span></div>
    <div class="editor-label"><label for="ImageUpload_FileName">FileName</label></div>
    <div class="editor-field"><input name="ImageUpload.FileName" class="text-Box single-line" id="ImageUpload_FileName" type="text" value=""> <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="ImageUpload.FileName"></span></div>

    <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="ImageUpload"></span>
</div>

解决方法

将图像控件更改为此,
@Html.TextBoxFor(m => m.ImageUpload,new { type = "file",name = "Files" })
原文链接:/aspnet/252041.html

猜你在找的asp.Net相关文章