EditorFor()可以用来创建吗?
给定这个模型,是否可以使用 Html.EditorFor() 将文件上传输入元素渲染到页面?我尝试了属性 FileName 的数据类型,它肯定会影响呈现的编辑器表单。
public class DR405Model
{
[DataType(DataType.Text)]
public String TaxPayerId { get; set; }
[DataType(DataType.Text)]
public String ReturnYear { get; set; }
public String FileName { get; set; }
}
强类型 *.aspx 页面如下所示
<div class="editor-field">
<%: Html.EditorFor(model => model.FileName) %>
<%: Html.ValidationMessageFor(model => model.FileName) %>
</div>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是 MVC 5 的示例(htmlAttributes 必需)。
在 ~\Views\Shared\EditorTemplates 下将其创建为名为 HttpPostedFileBase.cshtml 的文件。
这将生成具有正确 ID 和名称的控件,并在从模型 EditorFor 模板编辑集合时起作用。
Here's an example for MVC 5 (required for the htmlAttributes).
Create this as a file called HttpPostedFileBase.cshtml under ~\Views\Shared\EditorTemplates
This generates the control with the correct id and name and works when editing collections from a model EditorFor template.
添加:
htmlAttributes = new { type = "file" }
注意:我使用的是 MVC 5,我没有在其他版本上进行测试。
Add:
htmlAttributes = new { type = "file" }
Note: I'm using MVC 5, I have not tested on other versions.
不,但看看 http://haacked.com /archive/2010/07/16/uploading-files-with-aspnetmvc.aspx
No but take a look at http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx
使用 HttpPostedFileBase 来表示视图中上传的文件会更有意义model 而不是
string
:那么你可以有以下视图:
最后在
~/Views/Shared/EditorTemplates/HttpPostedFileBase.ascx
中定义相应的编辑器模板:现在是控制器可能看起来像这样:
It would make more sense to use HttpPostedFileBase to represent an uploaded file on your view model instead of
string
:then you could have the following view:
And finally define the corresponding editor template inside
~/Views/Shared/EditorTemplates/HttpPostedFileBase.ascx
:Now the controller might look like this: