2013-02-23 37 views
14

đây là ViewModel của tôiHttpPostedFileBase không ràng buộc để mô hình

public class FaultTypeViewModel 
{ 
    [HiddenInput(DisplayValue = false)] 
    public int TypeID { get; set; } 

    [Required(ErrorMessageResourceType = typeof(AdministrationStrings), ErrorMessageResourceName = "FaultTypeNameRequired")] 
    [Display(ResourceType = typeof(AdministrationStrings), Name = "FaultTypeName")] 
    public string TypeName { get; set; } 

    [Display(ResourceType = typeof(AdministrationStrings), Name = "FaultTypeDescription")] 
    [DataType(DataType.MultilineText)] 
    public string TypeDescription { get; set; } 

    [Display(ResourceType = typeof(AdministrationStrings), Name = "FaultTypeImageFile")] 
    public HttpPostedFileBase TypeImageFile { get; set; } 

    [HiddenInput(DisplayValue = false)] 
    public string TypeImageURL { get; set; } 
} 

Thông báo Tôi có một "TypeImageFile" HttpPostedFileBase Tôi hy vọng rằng các mô hình chất kết dính sẽ gắn bó tài sản đó từ hình thức đến mô hình đi vào bu khiển Tôi chỉ tiếp tục nhận được null.

đây là mã có liên quan trong xem:

@using (Html.BeginForm("AddFaultType","Administration", FormMethod.Post)) 
{ 

    <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> 
      ×</button> 
     <h3 id="myModalLabel">@SharedStrings.Add @SharedStrings.FaultType</h3> 
    </div> 
    <div class="modal-body"> 
     @Html.ValidationSummary(true) 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.TypeName) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.TypeName) 
      @Html.ValidationMessageFor(model => model.TypeName) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.TypeDescription) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.TypeDescription) 
      @Html.ValidationMessageFor(model => model.TypeDescription) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.TypeImageFile) 
     </div> 
     <div class="editor-field"> 
      <input type="file" name="TypeImageFile" id="TypeImageFile" /> 
     </div> 
    </div> 

    <div class="modal-footer"> 
     <input type="submit" value="@SharedStrings.Add" class="btn btn-primary" /> 
     @Html.ActionLink(SharedStrings.Cancel, "Index", "Administration", null, new { Class = "btn", data_dismiss = "modal", aria_hidden = "true" }) 
    </div> 
} 

và đây là bộ điều khiển:

 [HttpPost] 
     public ActionResult AddFaultType(FaultTypeViewModel i_FaultToAdd) 
     { 
      var fileName = Path.GetFileName(i_FaultToAdd.TypeImageFile.FileName); 
      var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName); 
      i_FaultToAdd.TypeImageFile.SaveAs(path); 

      return RedirectToAction("Index"); 
     } 
+0

chúc mừng bạn. vui lòng bao gồm biểu mẫu của bạn trong chế độ xem của bạn. –

+1

@DaveA Cảm ơn Dave! :) ... Chúc mừng Purim cho bạn là tốt, tôi đã thêm mã xem đầy đủ bao gồm các hình thức. – Mortalus

Trả lời

37

Hãy chắc chắn rằng bạn đã thiết lập các thuộc tính enctype vào mẫu của bạn để multipart/form-data vào mẫu của bạn nếu bạn muốn có thể tải lên các tệp:

@using (Html.BeginForm("AddFaultType", "Administration", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    ... 
} 
+0

nên làm điều đó –

+3

Đã có thiết lập này đúng cách. Vẫn chỉ nhận được null. – Trevor

+0

Điều này có thể được thực hiện với 'Ajax.BeginForm'? – Tyler

Các vấn đề liên quan