2

データベースから動的に生成される複数ページのフォームにユーザーが入力できるサイトを構築しています。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>
} 

jQuery ajax に置き換えられた部分ビュー (_FormPartial) は、簡略化されています。

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

3 に答える 3

1

クラスanswer内のの型をnotに変更します。QuestionPageItemHttpPostedFiledBase object

public HttpPostedFileBase answer { get; set; }

実際:彼の本当の問題は、彼がフォームを jQuery の Ajax リクエストの一部として送信しようとしたことです。

于 2013-03-13T15:16:51.237 に答える
0

mattytommo が述べたように、この方法ではファイルのアップロードはできません。

JQueryプラグインを使用して、ページに投稿せずにファイルをアップロードすると思います。

mattytommo の助けに感謝します。

質問はコメントで解決されているため、彼の回答を正しいとマークする必要があるかどうかわかりません。

于 2013-03-13T16:58:51.420 に答える
0

ファイル フィールドはインデックス化する必要があります。そうしないと、サーバーにアップロードされません

<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" />
于 2021-11-22T06:12:58.020 に答える