EditorFor()可以用来创建吗?

发布于 2024-11-09 14:30:06 字数 578 浏览 6 评论 0原文

给定这个模型,是否可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

也只是曾经 2024-11-16 14:30:07

以下是 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 模板编辑集合时起作用。

Here's an example for MVC 5 (required for the htmlAttributes).

Create this as a file called HttpPostedFileBase.cshtml under ~\Views\Shared\EditorTemplates

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

This generates the control with the correct id and name and works when editing collections from a model EditorFor template.

忆伤 2024-11-16 14:30:07

添加: 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,我没有在其他版本上进行测试。

Add: htmlAttributes = new { type = "file" }

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

Note: I'm using MVC 5, I have not tested on other versions.

ˉ厌 2024-11-16 14:30:06

使用 HttpPostedFileBase 来表示视图中上传的文件会更有意义model 而不是 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");
    }
}

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");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文