0

ユーザーがデータを入力してアップロードするファイルを選択する単純なフォームに取り組んでいます。しかし、私はこれを機能させることができません..何らかの理由で、[保存] をクリックしても、ファイルがコントローラーに移動しません。

ここにいくつかのコードがあります。

@using (Ajax.BeginForm("Add", "Category", null, new AjaxOptions
{
UpdateTargetId = "upload-message",
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
OnSuccess = "uploadSuccess"
}, new { id = "AddCategoryForm", enctype = "multipart/form-data" }))
{
<div class="editorLabel">
    @Html.LabelFor(m=>m.CategoryName)
</div>
<div class="editorText">
    @Html.TextBoxFor(m=>m.CategoryName)
</div>
<div class="editorLabel">
    @Html.LabelFor(m => m.Description)
</div>
<div class="editorText">
    @Html.TextAreaFor(m => m.Description)
</div>

<div class="editorLabel">
    @Html.LabelFor(m => m.IconPath)
</div>
<div class="editorText">
    <input type="file" id="file" name="file" />
</div>
<div class="editorLabel">
    @Html.LabelFor(m => m.IsActive)
</div>
<div class="editorText">
    @Html.CheckBoxFor(m=>m.IsActive)
</div>
<p>
    <input type="submit" id="submit" value="Save" />
</p>

}

コントローラ:

[HttpPost]
    public ActionResult Add(HttpPostedFileBase file,CategoryViewModel model)
    {
        if (ModelState.IsValid)
        {
            System.IO.FileInfo info = new FileInfo(file.FileName);
            string ext = info.Extension;
            //other code
         }
      }

ここのコントローラでは、file は常に null です。私はどこで間違っていますか??

4

1 に答える 1

0

最初にコントローラーのパラメーターを交換して、次のようにします。

[HttpPost]
public ActionResult Add(CategoryViewModel model, HttpPostedFileBase file)

たとえば。_

何らかの理由でこれが機能しない場合は、アップロードするファイルをモデル( CategoryViewModel) の一部にし、次のようにコントローラーの署名を付けます。

[HttpPost]
public ActionResult Add(CategoryViewModel model)

たとえば。_ そうすれば、ファイルはモデルの一部として返され、モデルのプロパティの 1 つとして抽出されます。これはうまくいきます(私にとってはうまくいきました)。

お役に立てれば。

于 2012-09-15T00:03:20.993 に答える