2011-05-24 71 views
21

鑑於此模型,是否可以使用Html.EditorFor()將文件上載輸入元素呈現給頁面?我玩弄了屬性FileName的數據類型,這絕對會影響呈現的編輯器表單。Can EditorFor()可用於創建<input type =「file」>?

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> 
+4

MVC4 - 我一直想知道爲什麼DataType.Upload不呈現類型=「文件」 – felickz 2012-10-04 05:29:50

回答

35

這將更有意義使用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"); 
    } 
} 
+0

謝謝你是我正在尋找的答案。 – 2011-05-25 12:17:43

+8

你也可以使用'@ Html.TextBoxFor(x => x.File,new {type =「file」})' – 2013-07-04 15:33:25

+0

@Darin - 如果我在數據庫中保存File url,將會是什麼類型的數據。在模型中,你有 - 「HttpPostedFileBase」 – 2016-05-03 00:48:23

0
+0

已經建成。也許我走錯了方向。我有aspnet_db配置文件數據,我需要與上傳的文件結婚。所以,用戶文件用'user_id' +'date' +'「xlsx」保存到服務器上我想我需要創建一個模型,它的屬性超出了文件對象的範圍。 – 2011-05-24 16:12:08

5

這裏是一個MVC 5的示例(htmlAttributes需要)。

一個名爲HttpPostedFileBase.cshtml下〜文件\視圖創建這個\共享\ EditorTemplates

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

這將生成正確的ID和名字的控制和編輯工作從模型EditorFor模板集合時。

+0

這裏很重要的一點是,在幕後可能會有更多的事情要做。上述內容在.cshtml文件屬性中的設置被切換之前無效:構建操作,無 - >內容。根據這個問題的回答:[鏈接](http://stackoverflow.com/questions/6782769/why-is-my-editor-template-ignored-when-i-call-editorformodel) – GG2 2016-06-05 23:01:30

+0

幾點,構建.cshtnl的行爲是默認的內容,你提到的答案是從2011年開始的,並且談到了關於MVC 3 - MVC 5我不得不做任何改變來使這項工作 – 2016-06-06 12:27:10

+0

工程就像微風。除了需要編輯器模板外,沒有額外的變化 – Nitin 2017-09-05 10:59:38

3

地址:htmlAttributes = {新類型= 「文件」}

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

注:我使用MVC 5,我還沒有在其它版本測試。

相關問題