2013-02-23 17 views
14

tutaj jest mój ViewModelHttpPostedFileBase nie wiąże modelować

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

Wskazówka Mam „TypeImageFile” HttpPostedFileBase Spodziewam się, że spoiwo modelu byłoby związać tę właściwość z formularza do modelu przechodzi do bu kontrolera Właśnie otrzymuję zero.

tutaj jest odpowiedni kod w widoku:

@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> 
} 

i tutaj jest kontrolerem:

 [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

szczęśliwy purim. dołącz swój formularz do swojego widoku. –

+1

@DaveA Dziękuję Dave! :) ... Wesołych Purim również dodałem pełny kod widoku wraz z formularzem. – Mortalus

Odpowiedz

37

Upewnij się, że ustawiony atrybut enctype na formularzu do multipart/form-data na formularzu jeśli chcesz mieć możliwość przesyłania plików:

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

które powinno to zrobić –

+3

Już to poprawnie ustawione. Nadal po prostu null. – Trevor

+0

Czy można to zrobić za pomocą 'Ajax.BeginForm'? – Tyler

Powiązane problemy