2017-05-21 2 views
2

Ich habe ein Upload-Formular, und ich mag, dass meine Informationen wie ein Bild und ein anderes Feld passieren, aber ich weiß nicht, wie ich Bild hochladen ..Hochladen Bild in Asp.net Mvc-Controller

Dies ist Meine Modellklasse

public partial class NewProductCategory 
 
    { 
 
     public string ProductName { get; set; } 
 
     public string ProductDescription { get; set; } 
 
     public string ProductPrice { get; set; } 
 
     public string ProductImage { get; set; } 
 
     public string ProductQuantity { get; set; } 
 
     public Nullable<bool> ProductStatus { get; set; } 
 
     public Nullable<int> CategoryId { get; set; } 
 

 
     public HttpPostedFileBase user_image_data { get; set; } 
 
     public virtual Category Category { get; set; } 
 

 
    }

[HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Create(NewProductCategory productcategory, HttpPostedFileBase file) 
     { 
      if (ModelState.IsValid) 
      { 
       ProductCategory newproductCategory = new ProductCategory(); 
       string path = Path.Combine(Server.MapPath("~/Content/files"), Path.GetFileName(file.FileName)); 
       file.SaveAs(path); 
       newproductCategory.ProductDescription = productcategory.ProductDescription; 
       newproductCategory.ProductQuantity = productcategory.ProductQuantity; 
       newproductCategory.ProductStatus = productcategory.ProductStatus; 

       newproductCategory.CategoryId = productcategory.CategoryId; 

       db.ProductCategories.Add(newproductCategory); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Name", productcategory.CategoryId); 
      return View(productcategory); 
     } 

Und dies ist mein Code anzeigen

@model MvcApplication1.Models.NewProductCategory 
@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 

@using (Html.BeginForm()) { 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <legend>ProductCategory</legend> 
     @using (Html.BeginForm("Create", "Temp", FormMethod.Post, new { enctype = "multipart/form-data" })) 

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

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

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

     <div class="editor-label"> 
      @Html.LabelFor(model => model.user_image_data) 
     </div> 
     <div class="editor-label"> 
       @Html.Label("Upload your image") 
      </div> 


      <div class="editor-field"> 
       @Html.TextBoxFor(model => model.user_image_data, new { Type = "File" }) 
       @Html.ValidationMessageFor(model => model.user_image_data) 
      </div> 

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

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

     <div class="editor-label"> 
      @Html.LabelFor(model => model.CategoryId, "Category") 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("CategoryId", String.Empty) 
      @Html.ValidationMessageFor(model => model.CategoryId) 
     </div> 

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

Bitte helfen Sie mir in Controller ich erhalte Nullwert für Datei-Upload

+1

Da Sie auf eine Eigenschaft mit dem Namen 'user_image_data' (nicht' file') Bindung aber du hast dein Modell nicht gezeigt, also wissen wir nicht, was das ist - es sollte type 'HttpPostedFileBase' sein. Wenn Sie an einen Parameter mit dem Namen 'file' binden wollen, dann benennen Sie den Eingang' file' –

+0

Ya ich hatte getan HttpPostedFileBase, aber ich habe ein Problem, dass @model MvcApplication1.Models.NewProductCategory kann es aber mit einer anderen Eingabe ausgeführt werden Feld es bekommen Fehler und ok, ich werde bearbeiten und fügen Sie meine Modellklasse zu –

Antwort

0

aktualisieren Ansawer

@model MvcApplication1.Models.NewProductCategory 
@{ 
    ViewBag.Title = "Create"; 
} 

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

    { @Html.AntiForgeryToken() 
     @Html.ValidationSummary(true) 

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

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

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

     <div class="editor-label"> 
      @Html.LabelFor(model => model.user_image_data) 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(model => model.user_image_data, new { Type = "File" }) 
      @Html.ValidationMessageFor(model => model.user_image_data) 
     </div> 
     <div class="editor-label"> 
      @Html.Label("Upload your image") 
     </div> 
     <div class="editor-label"> 
      @Html.TextBox("file",null,htmlAttributes: new { Type = "file" }) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.ProductQuantity) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.ProductQuantity) 
      @Html.ValidationMessageFor(model => model.ProductQuantity) 
     </div> 

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

     <div class="editor-label"> 
      @Html.LabelFor(model => model.CategoryId, "Category") 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("CategoryId", String.Empty) 
      @Html.ValidationMessageFor(model => model.CategoryId) 
     </div> 
     <p> 
      <input type="submit" value="Create" /> 
     </p> 
     </fieldset> 
    } 
+0

Ich habe oben Änderungen vorgenommen, immer noch null Wert in httpHttpPostedFileBase Datei, was nun andere Option, Ich habe einfach eine Demo ohne die Modellklasse erstellt es funktioniert gut , aber beim Hinzufügen und Übergeben mit (Produktkategorie NewProductCategory, Datei HttpPostedFileBase) erhält diese Klasse den Wert Null –

+0

@ILYASPATEL Ich aktualisiere meine Antwort. – Ashiquzzaman

+0

Thnx viel funktioniert :) –