且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

EditorFor()可用于创建和LT;输入类型="文件"&GT ;?

更新时间:2022-11-30 10:47:59

这将更有意义使用HttpPostedFileBase重新present上传文件,您的视图模型,而不是字符串

 公共类DR405Model
{
    [数据类型(DataType.Text)
    公共字符串TaxPayerId {搞定;组; }    [数据类型(DataType.Text)
    公共字符串ReturnYear {搞定;组; }    公共HttpPostedFileBase文件{搞定;组; }
}

然后你可以有以下几种观点:

 <%使用(Html.BeginForm(指数,家,FormMethod.Post,新{ENCTYPE =的multipart / form-data的})){%GT ;    ......其他看法模特属性输入字段    < D​​IV 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");
    }
}