Sha*_*dil 1 c# asp.net-mvc razor asp.net-mvc-5
View生成3个文件输入字段.这是截图:
但是,当我添加EditorFor模板时HttpPostedFileBase,它完美地工作.我想知道为什么会这样.
这是我的模特:
public class UploadFileViewModel
{
[Required]
[Display(Name ="Select Excel File")]
public HttpPostedFileBase ExcelFile { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
控制器:
public class HomeController : Controller
{
public ActionResult UploadFileData()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
风景:
@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>
}
Run Code Online (Sandbox Code Playgroud)
小智 6
EditorFor()在复杂对象上使用会为对象的每个属性生成默认模板(包括标签,表单控件和验证消息占位符).HttpPostedFileBase包含属性ContentType,ContentLength和FileNameSO 3级的输入产生的.由于您已经type="file"在additionalViewData,所产生的输入是type="file".
你可以简单地使用
@Html.TextBoxFor(m => m.ExcelFile, new { type = "file" })
Run Code Online (Sandbox Code Playgroud)
或者您可以EditorTemplate为typeof 创建自定义,HttpPostedFileBase以便EditorFor()使用该模板,而不是默认模板.但是,该模板需要包括@Html.TextBoxFor(m => m, new { type = "file" })所以可能没有多大意义,除了对包括 LabelFor()和ValidationMessageFor()与封闭<div>的元素,使所有需要在视图中@Html.EditorFor(m => m.ExcelFile)生成所有的HTML.这可以简化主视图,但缺点是您不能col-md-10在一个视图和col-md-8另一个视图中拥有.
| 归档时间: |
|
| 查看次数: |
594 次 |
| 最近记录: |