2014-02-24 16 views
10

Ich habe diesen Controller und was ich versuche zu tun ist, um ein Bild an die Steuerung als [Byte] zu senden, das ist mein Controller:MVC HttpPostedFileBase immer null

[HttpPost] 
public ActionResult AddEquipment(Product product, HttpPostedFileBase image) 
{ 

      if (image != null) 
      { 
       product.ImageMimeType = image.ContentType; 
       product.ImageData = new byte[image.ContentLength]; 
       image.InputStream.Read(product.ImageData, 0, image.ContentLength); 
      } 

      _db.Products.Add(product); 
      _db.SaveChanges(); 

      return View(); 
} 

und auf meiner Ansicht:

@using (Html.BeginForm("AddEquipment", "Equipment", FormMethod.Post)) { 

    <fieldset> 
     <legend>Product</legend> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Name) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Name) 
      @Html.ValidationMessageFor(model => model.Name) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Description) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Description) 
      @Html.ValidationMessageFor(model => model.Description) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Price) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Price) 
      @Html.ValidationMessageFor(model => model.Price) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Category) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Category) 
      @Html.ValidationMessageFor(model => model.Category) 
     </div> 

     <div> 
     <div>IMAGE</div> 
     <input type="file" name="image" /> 

     </div> 


     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
} 

aber das Problem ist, dass der Wert für Bild auf meinem Controller alway null ist, kann ich nicht scheinen keine Informationen über die HttpPostedFileBase zu bekommen

Antwort

23

Sie müssen die encType mit mu hinzufügen lipart/Formulardaten.

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

Sie können immer fügen Sie es zu Ihrem Modell wie folgt, es bietet ein Ansichtsmodell:

public class Product 
{ 
    public Product() 
    { 
     Files = new List<HttpPostedFileBase>(); 
    } 

    public List<HttpPostedFileBase> Files { get; set; } 
    // Rest of model details 
} 

können Sie die Dateien abrufen, indem Sie die nicht benötigte Parameter dh

Entfernen
[HttpPost] 
public ActionResult AddEquipment(Product product) 
{ 
    var file = model.Files[0]; 
    ... 
} 
+0

Awesome! vielen Dank für die schnelle Antwort! –

+0

@RiquelmyMelara Keine Probs, ich werde die Antwort auch verbessern. – hutchonoid

+0

Sein „Produkt“ Objekt ist Ich denke, eine Entity Framework „Entity“ geben so eine andere Eigenschaft Hinzufügen könnte seinen Code brechen, wenn er nicht die Config auch –

0

Versuchen Sie, diese an der Spitze des Aktionsmethode zu tun:

[HttpPost] 
    public ActionResult AddEquipment(Product product, HttpPostedFileBase image) 
    { 
     image = image ?? Request.Files["image"]; 
     // the rest of your code 
    } 

Und die Form sollte enctype von "multipart/form-data", um Dateien zu:

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