簡體   English   中英

MVC 4 動態表單文件上傳

[英]Mvc 4 dynamic form file upload

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

問題是將文件發布到我的控制器,我正在使用這篇文章中的 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[]{"test"} 之類的東西,文件字段返回 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);
            }
        });
    });

QuestionPageItem類內部的answer類型更改為HttpPostedFiledBase 而不是 object

public HttpPostedFileBase answer { get; set; }

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

正如mattytommo提到的那樣,無法通過這種方式上傳文件。

我想我將使用JQuery插件上傳文件,而仍在頁面上而不發布它。

我感謝mattytommo的幫助。

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

文件字段必須被索引,否則不會上傳到服務器

<input type="file" name="Document_Path[0]" class="form-control" />
<input type="file" name="Document_Path[1]" class="form-control" />
<input type="file" name="Document_Path[2]" class="form-control" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM