2013-03-13 60 views
2

我正在構建一個網站,用戶可以填寫從數據庫動態生成的多頁表單。我使用JQuery將數據發佈到我的控制器並返回表單的下一頁。它適用於除文件之外的所有內容。Mvc 4動態表單文件上傳

問題是將文件發佈到我的控制器,我使用this post的HtmlHelpers生成文件字段的html。

我的模型:

public class QuestionPage 
{ 
    public const string Next = "next"; 
    public const string Prev = "prev"; 
    public const string Save = "save"; 

    public int currentID { get; set; } 
    public bool advanced { get; set; } 
    public QuestionPageItem[] questions { get; set; } 
    public int pagenumber { get; set; } 
    public int? next { get; set; } 
    public int? prev { get; set; } 

    public string submitaction { get; set; } 
    public int? gotoID { get; set; } 

    public Dictionary<Int32, QuestionPageTitle> titles { get; set; } 
} 

public class QuestionPageItem 
{ 
    public int id { get; set; } 
    public string question { get; set; } 
    public string description { get; set; } 
    public bool required { get; set; } 
    public QuestionType type { get; set; } 
    public object answer { get; set; } 
    public int? enableID { get; set; } 
    public bool initHidden { get; set; } 
    public QuestionOptionIndexModel[] options { get; set; } 
} 

我的靜態視圖:

using (Html.BeginForm("Form", "QuestionPage", new { id = Model }, FormMethod.Post, new { id = "frm" + Model, name = Model, enctype = "multipart/form-data" })) 
{ 
    <div id="formcontainer"> 
     @Html.Partial("_FormPartial", Model) 
    </div> 
} 

我的局部視圖(_FormPartial)它獲取的jQuery AJAX替代,簡化了:

@model DPDF.Models.QuestionPage 
    Some hidden fields... 
    @for (int i = 0; i < Model.questions.Length; i++) 
    { 
     var question = Model.questions[i]; 
     Some hidden fields... 
     <tr class="@(question.initHidden ? "hidden" : "")" id="@(question.id)" > 
      show textbox or textarea or radio etc depending on question type, for instance 
      @Html.TextBox("questions[" + i + "].answer", (question.answer == null ? string.Empty : question.answer.ToString())) 
      in case of file field 
      @Html.FileBox("questions[" + i + "].answer") 
     </tr> 
    } 

的數據獲取綁定到問題對象中的答案字段。 我的控制器動作:

[AllowAnonymous] 
    public ActionResult Form(QuestionPage model) 
    { 

     if (!ModelState.IsValid) 
      return View(model); 

     // this is to make some hidden fields get the correct new values 
     ModelState.Clear(); 

     // do stuff to determine what page to show next... 
     // save data, extract value from model.answer object depending on question type 
     // make new model and return it 
     if (Request.IsAjaxRequest()) 
      return PartialView("_FormPartial", model); 
     return View(model); 
    } 

像文本框普通領域返回類似的String [] { 「測試」},文件中的字段返回null。編輯: 也許問題出在JavaScript?這是代碼發送到服務器:

var $form = $(form); 
    var $container = $('#formcontainer'); 
    $container.hide('slide', { direction: direction }, 500, function() { 
     $.ajax({ 
      url: $form.attr('action'), 
      type: $form.attr('method'), 
      data: $form.serialize(), 
      success: function (data, textStatus, jqXHR) { 
       $container.html(data); 
       updateOverzicht(); 
       $container.show('slide', { direction: (direction == 'left') ? 'right' : 'left' }, 500); 
      } 
     }); 
    }); 

回答

1

變化answerQuestionPageItem類中的類型是HttpPostedFiledBaseobject

public HttpPostedFileBase answer { get; set; } 

實際上是:他的真正的問題是因爲他試圖在jQuery中提交他的表單作爲Ajax請求的一部分。

+0

這項工作將從複選框列表中檢索文本框和字符串數組中的字符串嗎? – Blight 2013-03-13 15:39:55

+0

@Blight Nope,爲此,您必須爲每個字段提供'@ Html.HiddenFor(m => Model.questions [i] .FieldName)' – mattytommo 2013-03-13 15:43:35

+0

您能否告訴我如何檢索發佈的信息由隱藏字段的用戶? – Blight 2013-03-13 15:50:32

0

正如mattytommo所提到的,文件上傳不可能以這種方式進行。

我想我會使用JQuery插件上傳文件,同時仍然在頁面上發佈。

我感謝mattytommo的幫助。

我不確定我是否應該將他的答案標記爲正確,因爲問題已在評論中得到解決。

+0

檢查我的答案,我已編輯,以包括該問題:) – mattytommo 2013-03-14 09:37:10