【问题标题】:Can EditorFor() be used to create <input type="file">?EditorFor() 可以用来创建 <input type="file"> 吗?
【发布时间】:2011-09-01 02:04:57
【问题描述】:

鉴于此模型,是否可以使用 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>

【问题讨论】:

  • MVC4 - 我一直想知道为什么 DataType.Upload 不呈现 type="file"

标签: c# asp.net-mvc asp.net-mvc-2 editorfor


【解决方案1】:

【讨论】:

  • 已经建好了。也许我走错了方向。我有 aspnet_db 配置文件数据,我需要与上传的文件结合。因此,用户文件使用user_id + date + "xlsx" 保存到服务器我想我需要创建一个模型,它具有文件对象之外的属性。
【解决方案2】:

使用HttpPostedFileBase 代替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; }
}

那么你可以有以下视图:

<% 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" />
<% } %>

最后在~/Views/Shared/EditorTemplates/HttpPostedFileBase.ascx里面定义对应的编辑器模板:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<input type="file" name="<%: ViewData.TemplateInfo.GetFullHtmlFieldName("") %>" id="<%: ViewData.TemplateInfo.GetFullHtmlFieldId("") %>" />

现在控制器可能如下所示:

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");
    }
}

【讨论】:

  • 谢谢,这就是我正在寻找的答案。
  • 你也可以使用@Html.TextBoxFor(x =&gt; x.File, new { type = "file" })
  • @Darin - 如果我将文件 url 保存在数据库中,数据类型将是什么。在模型中你有 - “HttpPostedFileBase”
【解决方案3】:

这是 MVC 5 的示例(htmlAttributes 必需)。

在 ~\Views\Shared\EditorTemplates 下创建一个名为 HttpPostedFileBase.cshtml 的文件

@model HttpPostedFileBase
@{
    var htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData["htmlAttributes"]);
    htmlAttributes["type"] = "file";
}
@Html.TextBoxFor(model => model, htmlAttributes)

这会生成具有正确 ID 和名称的控件,并在从模型 EditorFor 模板编辑集合时工作。

【讨论】:

  • 这里似乎很重要的是要注意幕后可能会有更多事情发生。在 .cshtml 文件的属性中的设置被切换之前,上述内容将不起作用:构建操作,无 --> 内容.根据对此问题的回复:link
  • 几点,.cshtnl 的构建操作默认为 Content ,您所指的答案是从 2011 年开始讨论 MVC 3 - MVC 5 我必须不做任何更改才能完成这项工作
  • 像微风一样工作。除了需要编辑器模板外,无需额外更改
【解决方案4】:

地址:htmlAttributes = new { type = "file" }

<div class="editor-field">
    <%: Html.EditorFor(model => model.FileName, new { htmlAttributes = new { type = "file" }}) %>
    <%: Html.ValidationMessageFor(model => model.FileName) %>
</div>

注意:我使用的是 MVC 5,我还没有在其他版本上测试过。

【讨论】:

    猜你喜欢
    • 2011-02-05
    • 2013-06-13
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 2017-12-04
    • 2011-03-27
    • 2011-02-18
    相关资源
    最近更新 更多