问题描述
由于这种模式,是否有可能使用Html.EditorFor()来渲染一个文件上传输入元素的网页?我打得四处属性文件名的数据类型,它肯定是影响编辑的形式呈现。
公共类DR405Model
{
[数据类型(DataType.Text)
公共字符串TaxPayerId {搞定;组; }
[数据类型(DataType.Text)
公共字符串ReturnYear {搞定;组; } 公共字符串文件名{搞定;组; }
}
强类型的* .aspx页面中看起来像这样
< DIV CLASS =主编场>
<%:Html.EditorFor(型号=> model.FileName)%GT;
<%:Html.ValidationMessageFor(型号=> model.FileName)%GT;
< / DIV>
这将更有意义使用HttpPostedFileBase重新present上传文件,您的视图模型,而不是字符串
:
公共类DR405Model
{
[数据类型(DataType.Text)
公共字符串TaxPayerId {搞定;组; } [数据类型(DataType.Text)
公共字符串ReturnYear {搞定;组; } 公共HttpPostedFileBase文件{搞定;组; }
}
然后你可以有以下几种观点:
<%使用(Html.BeginForm(指数,家,FormMethod.Post,新{ENCTYPE =的multipart / form-data的})){%GT ; ......其他看法模特属性输入字段 < DIV CLASS =主编场>
<%= Html.EditorFor(型号=> model.File)%GT;
<%= Html.ValidationMessageFor(型号=> model.File)%GT;
< / DIV> <输入类型=提交VALUE =OK/>
<%}%GT;
最后确定内部相应的编辑模板〜/查看/共享/ EditorTemplates / HttpPostedFileBase.ascx
:
<%@控制语言=C#继承=System.Web.Mvc.ViewUserControl%GT;
<输入类型=文件名称=<%:ViewData.TemplateInfo.GetFullHtmlFieldName()%GT; ID =下;%:ViewData.TemplateInfo.GetFullHtmlFieldId()%>中/>
现在控制器可能是这样的:
公共类HomeController的:控制器
{
公众的ActionResult指数()
{
返回查看(新DR405Model());
} [HttpPost]
公众的ActionResult指数(DR405Model模型)
{
如果(model.File =空&放大器;!&放大器; model.File.ContentLength大于0)
{
变种文件名= Path.GetFileName(model.File.FileName);
VAR路径= Path.Combine(使用Server.Mappath(〜/ App_Data文件),文件名);
model.File.SaveAs(路径);
} 返回RedirectToAction(「指数」);
}
}
Given this model, is it possible to use the Html.EditorFor() to render a file upload input element to the page? I played around with the Datatype of the property FileName, and it was definitely impacting the editor form rendered.
public class DR405Model
{
[DataType(DataType.Text)]
public String TaxPayerId { get; set; }
[DataType(DataType.Text)]
public String ReturnYear { get; set; }
public String FileName { get; set; }
}
Strongly Typed *.aspx page looks like this
<div class="editor-field">
<%: Html.EditorFor(model => model.FileName) %>
<%: Html.ValidationMessageFor(model => model.FileName) %>
</div>
It would make more sense to use HttpPostedFileBase to represent an uploaded file on your view model instead of string
:
public class DR405Model
{
[DataType(DataType.Text)]
public string TaxPayerId { get; set; }
[DataType(DataType.Text)]
public string ReturnYear { get; set; }
public HttpPostedFileBase File { get; set; }
}
then you could have the following view:
<% using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { %>
... input fields for other view model properties
<div class="editor-field">
<%= Html.EditorFor(model => model.File) %>
<%= Html.ValidationMessageFor(model => model.File) %>
</div>
<input type="submit" value="OK" />
<% } %>
And finally define the corresponding editor template inside ~/Views/Shared/EditorTemplates/HttpPostedFileBase.ascx
:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<input type="file" name="<%: ViewData.TemplateInfo.GetFullHtmlFieldName("") %>" id="<%: ViewData.TemplateInfo.GetFullHtmlFieldId("") %>" />
Now the controller might look like this:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new DR405Model());
}
[HttpPost]
public ActionResult Index(DR405Model model)
{
if (model.File != null && model.File.ContentLength > 0)
{
var fileName = Path.GetFileName(model.File.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data"), fileName);
model.File.SaveAs(path);
}
return RedirectToAction("Index");
}
}
这篇关于EditorFor()可用于创建和LT;输入类型=&QUOT;文件&QUOT;&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!