0

Uploadify を使用してサーバーにファイルをアップロードしようとしていますが、TempData を使用してコントローラー間で変数を渡すことはできず、エラーは見つかりませんでした。

コントローラー「GetFile」のTempDataで変数fileNameとfileをコントローラー「ModelCreate」に渡そうとしています。

コントローラー「GetFile」はうまくいきますが、コントローラー「ModelCreate」の「date1」と「date2」の値を確認するとnullです

コントローラーに保存したファイルを「ModelCreate」にしたいだけ

  public string GetFile(HttpPostedFileBase file)
        {
            var fileName = this.Server.MapPath("~/Informs/" + System.IO.Path.GetFileName(file.FileName));

            if (System.IO.File.Exists(fileName))
                return "has been uploaded successfully";
            file.SaveAs(fileName);

            TempData["NameFile"] = fileName;
            TempData["File"] = file;
            return "1";
        }


        [HttpPost]
        public ActionResult ModelCreate(INFORME inform)
       {
            var date1 = TempData["NameFile"] as string;
            var date2 = TempData["File"] as HttpPostedFileBase;
            date2.SaveAs(date1);
        .
        .
        .
        .
        }

「date1」と「date2」がnullなのはなぜですか?

祝福

4

1 に答える 1

2

この質問に答えるのに十分な情報がありません。コメント セクションで要求されたように、ユーザーがいくつかの入力フィールドに入力してファイルをアップロードできるようにするフォームを示す完全な例を提供します。

いつものように、ビューに表示したい情報を反映するビューモデルを定義することから始めます:

public class MyViewModel
{
    [Required]
    public string TextField { get; set; }

    [DataType(DataType.MultilineText)]
    public string TextAreaField { get; set; }

    public bool CheckBoxField { get; set; }

    [Required]
    public HttpPostedFileBase FileField { get; set; }
}

次に、フォームを表示するだけの GET アクションと、送信時にフォーム情報を処理する POST アクションの 2 つのアクションを持つコントローラーを作成できます。

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel();
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        if (!ModelState.IsValid)
        {
            // there were validation errors => redisplay the view
            return View(model);
        }

        // at this stage the model is valid => we could do some processing

        // first let's save the file
        var appData = Server.MapPath("~/app_data");
        var file = Path.Combine(appData, Path.GetFileName(model.FileField.FileName));
        model.FileField.SaveAs(file);

        // then we could process the other properties
        // ...

        return Content("Thanks for submitting the data");
    }
}

そして最後に、ビューモデルの上に強く型付けされたビュー:

@model MyViewModel

@Html.ValidationSummary(false)

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div>
        @Html.EditorFor(x => x.TextField)
        @Html.ValidationMessageFor(x => x.TextField)
    </div>

    <div>
        @Html.EditorFor(x => x.TextAreaField)
        @Html.ValidationMessageFor(x => x.TextAreaField)
    </div>

    <div>
        @Html.CheckBoxFor(x => x.CheckBoxField)
        @Html.ValidationMessageFor(x => x.CheckBoxField)
    </div>

    <div>
        @Html.LabelFor(x => x.FileField)
        @Html.TextBoxFor(x => x.FileField, new { type = "file" })
    </div>

    <button type="submit">OK</button>
}
于 2012-05-28T08:11:44.363 に答える