文件上传后 Viewmodel 始终为 null

Viewmodel always null following file upload

我正在 Asp.Net MVC 中创建一个页面,用户可以在其中上传文件,以及来自文本框的文件的详细信息。

为此,我使用了一个视图模型和一个非常简单的方法配置。 [Get] 方法正确填充了页面,用户可以选择文件并输入详细信息,但是一旦单击 'Submit' 按钮并调用 [Post] 方法,视图模型完全为空。

我已经对此进行了研究并尝试过:

无果。

这是我的控制器方法:

public ActionResult FileComment(int id)
{
    //set up [get] view

    return View("FileComment", obj);
}

//THIS METHOD's VIEWMODEL ALWAYS NULL
[HttpPost]
public ActionResult FileComment(CalibrationCommentViewModel file)
{
    //save file to database

    return View("Edit", new { id = file.id });
}

以下是我的部分观点:

@using (Html.BeginForm("FileComment", "Calibration", FormMethod.Post, new { enctype = "multipart/form-data", @data_ajax = "false" }))
{
    @Html.HiddenFor(m => m.ID)
    <div class="container">
        <h4>Add File for @Model.ID</h4>
        <hr />
        <div class="row">
            <div class="col-md-1">g
                @Html.LabelFor(model => model.file, htmlAttributes: new { @class = "control-label" })
            </div>
            <div class="col-md-2">
                @Html.TextBoxFor(model => model.file, new { type = "file" })
            </div>
            <div class="col-md-9"></div>
        </div>
        <br />
        <div class="row">
            <div class="col-md-1">
                @Html.LabelFor(model => model.attachmentType, htmlAttributes: new { @class = "control-label" })
            </div>
            <div class="col-md-2">
                @Html.DropDownListFor(model => model.attachmentType, Model.attachTypeList, new { @class = "form-control" })
            </div>
            <div class="col-md-9"></div>
        </div>
        <br />
        <div class="row">
            <div class="col-md-1">
                @Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label" })
            </div>
            <div class="col-md-2">
                @Html.EditorFor(model => model.Date)
            </div>
            <div class="col-md-9"></div>
        </div>
        <br />

        <div class="row">
            <div class="col-md-1">
                @Html.LabelFor(model => model.description, htmlAttributes: new { @class = "control-label" })
            </div>
            <div class="col-md-2">
                @Html.TextAreaFor(model => model.description, new { cols=35, @rows=3})
            </div>
            <div class="col-md-9"></div>
        </div>

        <br />
        <div class="row">
            <div class="col-md-1"></div>
            <div class="col-md-2">
                <button type="submit" onclick="JavascriptFunction()">Add to @Model.ID</button>
            </div>
            <div class="col-md-9"></div>
        </div>

    </div>
}

改变

public ActionResult FileComment(CalibrationCommentViewModel file)

public ActionResult FileComment(CalibrationCommentViewModel model)

你的视图没有引用顶部的视图模型视图是这样的 @模型Nameofyourproject.Models.CalibrationCommentViewModel