2012-03-01 14 views
8

Sto solo cercando di creare un modulo in cui è possibile inserire un nome e caricare un file. Ecco il modello di vista:Passaggio di un HttpPostedFileBase a un metodo di controller

public class EmployeeViewModel 
{ 
    [ScaffoldColumn(false)] 
    public int EmployeeId { get; set; } 

    public string Name { get; set; } 

    public HttpPostedFileBase Resume { get; set; } 
} 

mio punto di vista:

@using (Html.BeginForm("Create", "Employees", FormMethod.Post)) 
{ 
    @Html.TextBoxFor(model => model.Name) 

    @Html.TextBoxFor(model => model.Resume, new { type = "file" }) 

    <p> 
     <input type="submit" value="Save" /> 
    </p> 

    @Html.ValidationSummary() 
} 

E il mio metodo di controllo:

[HttpPost] 
public ActionResult Create(EmployeeViewModel viewModel) 
{ 
    // code here... 
} 

Il problema è che quando ho posto al metodo di controllo, la proprietà Riprendi nullo. La proprietà Name viene passata correttamente, ma non HttpPostedFileBase.

Sto facendo qualcosa di sbagliato qui?

risposta

8

Aggiungere il enctype al form:

@Html.BeginForm("Create", "Employees", FormMethod.Post, 
       new{ enctype="multipart/form-data"}) 
2

Si prega di aggiungere Tipo di codifica in forma come,

@using (Html.BeginForm("Create","Employees",FormMethod.Post, new { enctype = "multipart/form-data" })) 
2

Aggiungere il tipo di codifica in forma di vista dal seguente codice:

@using (Html.BeginForm("Create", "Employees", FormMethod.Post,new{ enctype="multipart/form-data"})) 
{ 
    @Html.TextBoxFor(model => model.Name) 

    @Html.TextBoxFor(model => model.Resume, new { type = "file" }) 

    <p> 
    <input type="submit" value="Save" /> 
    </p> 
@Html.ValidationSummary() 
} 

Aggiungere il seguente codice nel rispettivo controller,

[HttpPost] 
public ActionResult Create(EmployeeViewModel viewModel) 
{ 
     if (Request.Files.Count > 0) 
     { 
      foreach (string file in Request.Files) 
      { 
       string pathFile = string.Empty; 
       if (file != null) 
       { 
        string path = string.Empty; 
        string fileName = string.Empty; 
        string fullPath = string.Empty; 
        path = AppDomain.CurrentDomain.BaseDirectory + "directory where you want to upload file";//here give the directory where you want to save your file 
        if (!System.IO.Directory.Exists(path))//if path do not exit 
        { 
         System.IO.Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "directory_name/");//if given directory dont exist, it creates with give directory name 
        } 
        fileName = Request.Files[file].FileName; 

        fullPath = Path.Combine(path, fileName); 
        if (!System.IO.File.Exists(fullPath)) 
        { 

         if (fileName != null && fileName.Trim().Length > 0) 
         { 
          Request.Files[file].SaveAs(fullPath); 
         } 
        } 
       } 
      } 
     } 
} 

ho asssumed percorso sarà all'interno della directory di basedirectory .... Si può dare il proprio percorso in cui si desidera salvare il file

+0

+1 che dire invece di Html.editor Html.TextBoxFor – Nikos

Problemi correlati