2017-05-21 6 views
2

은 내가 업로드 양식을하고 난 그런 이미지와 다른 필드로 내 정보를 전달하려는하지만 난 이미지를 업로드 할 수있는 방법을 모른다 내 모델 클래스업로드 이미지

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); 
     } 

그리고이 내보기 코드

입니다 당신이 user_image_data` (안`file`) '라는 이름의 속성에 바인딩하기 때문에 6,
@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> 
} 

컨트롤러에서 저를 도와주세요 난 파일 업로드

+1

을 Null 값을 얻고있다 하지만 모델을 표시하지 않았으므로 그 모델이 무엇인지 알지 못합니다.이 모델은 HttpPostedFileBase 유형이어야합니다. 'file'이라는 이름의 매개 변수에 바인드하고 싶다면 입력 파일의 이름을 –

+0

으로 지정하십시오. HttpPostedFileBase를 사용했는데, @model MvcApplication1.Models.NewProductCategory를 실행할 수 있지만 다른 입력으로 실행할 수 있습니다. 점점 그것을 필드 오류 와 내가 편집하고 잘 작동 너무 –

답변

0

업데이트 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

내 모델 클래스 내가 내가 단순히 모델 클래스하지 않고 데모를 만들어, 여전히 httpHttpPostedFileBase 파일, 이제 어떻게 다른 옵션에 null 값을 받고, 변경 위에 짓을 추가 할 것입니다 확인 ,하지만 (NewProductCategory productcategory, HttpPostedFileBase 파일)을 추가하고 전달하는 동안이 클래스는 null 값을 얻습니다. –

+0

@ILYASPATEL 답변을 업데이트합니다. – Ashiquzzaman

+0

Thnx 많이 작동 :) –