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

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

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

  1. @using (Html.BeginForm("Create","Event",FormMethod.Post,new { enctype = "multipart/form-data" }))
  2. {
  3. @Html.AntiForgeryToken()
  4.  
  5. <div class="form-horizontal">
  6. <div class="form-group">
  7. @Html.LabelFor(model => model.VenueId,new { @class = "control-label col-md-2" })
  8. <div class="col-md-10">
  9. @Html.EditorFor(model => model.VenueId)
  10. @Html.ValidationMessageFor(model => model.VenueId)
  11. </div>
  12. </div>
  13.  
  14. <div class="form-group">
  15. @Html.LabelFor(model => model.ImageUpload,new { @class = "control-label col-md-2" })
  16. <div class="col-md-10">
  17. @Html.EditorFor(model => model.ImageUpload)
  18. @Html.ValidationMessageFor(model => model.ImageUpload)
  19. </div>
  20. </div>
  21. }

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

  1. <div class="col-md-10">
  2. <div class="editor-label"><label for="ImageUpload_ContentLength">ContentLength</label></div>
  3. <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>
  4. <div class="editor-label"><label for="ImageUpload_ContentType">ContentType</label></div>
  5. <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>
  6. <div class="editor-label"><label for="ImageUpload_FileName">FileName</label></div>
  7. <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>
  8.  
  9. <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="ImageUpload"></span>
  10. </div>

解决方法

将图像控件更改为此,
  1. @Html.TextBoxFor(m => m.ImageUpload,new { type = "file",name = "Files" })

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