文件上传后,Viewmodel始终为空

时间:2015-04-28 21:08:27

标签: c# asp.net asp.net-mvc viewmodel

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

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

我已经完成了对此的研究,并尝试过:

  • 添加enctype =" multipart / form-data"
  • 只需将文件用作单独的参数

无济于事。

这是我的控制器方法:

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>
}

2 个答案:

答案 0 :(得分:0)

更改

public ActionResult FileComment(CalibrationCommentViewModel file)

public ActionResult FileComment(CalibrationCommentViewModel model)

答案 1 :(得分:0)

您的视图没有引用视图顶部的视图模型 @model Nameofyourproject.Models.CalibrationCommentViewModel