[英]Why is the HTML helper EditorFor of type file generating multiple file fields in C# and ASP.NET MVC?
View generates 3 file input fields. 视图将生成3个文件输入字段。 Here is the screenshot:
这是屏幕截图:
However, when I add EditorFor
template for HttpPostedFileBase
, then it works perfectly. 但是,当我为
HttpPostedFileBase
添加EditorFor
模板时,则可以完美运行。 I want to know why is this happening. 我想知道为什么会这样。
This is my model: 这是我的模型:
public class UploadFileViewModel
{
[Required]
[Display(Name ="Select Excel File")]
public HttpPostedFileBase ExcelFile { get; set; }
}
The controller: 控制器:
public class HomeController : Controller
{
public ActionResult UploadFileData()
{
return View();
}
}
The view: 风景:
@model DemoProject.Models.UploadFileViewModel
@{
ViewBag.Title = "Upload File Data";
}
<h2>Upload File Data</h2>
<p class="alert-success">@ViewBag.Success</p>
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken();
@Html.ValidationSummary("", new { @class = "text-danger" });
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model=>model.ExcelFile, htmlAttributes: new {@class="control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model=>model.ExcelFile, new { htmlAttributes = new { type = "file" } })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Upload" class="btn btn-default" />
</div>
</div>
</div>
}
Using EditorFor()
on a complex object generates the default template (which includes a label, form control and validation message placeholder) for each property of the object. 在复杂对象上使用
EditorFor()
为该对象的每个属性生成默认模板(包括标签,表单控件和验证消息占位符)。 HttpPostedFileBase
contains properties for ContentType
, ContentLength
and FileName
so 3 inputs are generated. HttpPostedFileBase
包含ContentType
, ContentLength
和FileName
属性,因此生成了3个输入。 Because you have included type="file"
in the additionalViewData
, the inputs generated are type="file"
. 因为已包含
type="file"
在additionalViewData
,所产生的输入是type="file"
。
You can simply use 您可以简单地使用
@Html.TextBoxFor(m => m.ExcelFile, new { type = "file" })
or you could create a custom EditorTemplate
for typeof HttpPostedFileBase
so that EditorFor()
uses that template, rather than the default template. 或者您可以为
HttpPostedFileBase
创建一个自定义EditorTemplate
,以便EditorFor()
使用该模板,而不是默认模板。 But that template would need to include @Html.TextBoxFor(m => m, new { type = "file" })
so there is probably not much point, other than to include the LabelFor()
and ValidationMessageFor()
and enclosing <div>
elements so that all that's needed in the view is @Html.EditorFor(m => m.ExcelFile)
to generate all the html. 但是该模板将需要包含
@Html.TextBoxFor(m => m, new { type = "file" })
所以除了包含LabelFor()
和ValidationMessageFor()
并包含<div>
元素,以便视图中所需的全部是@Html.EditorFor(m => m.ExcelFile)
以生成所有html。 This can simplify the main view, but the disadvantage is that you cannot for example, have col-md-10
in one view and col-md-8
in another. 这样可以简化主视图,但是缺点是您不能例如在一个视图中使用
col-md-10
在另一个视图中使用col-md-8
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.