【问题标题】:Viewmodel always null following file uploadViewmodel 在文件上传后始终为空
【发布时间】:2015-04-28 21:08:27
【问题描述】:

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

为此,我使用了一个视图模型和一个非常简单的方法配置。通过[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>
}

【问题讨论】:

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


    【解决方案1】:

    改变

    public ActionResult FileComment(CalibrationCommentViewModel file)
    

    public ActionResult FileComment(CalibrationCommentViewModel model)
    

    【讨论】:

      【解决方案2】:

      您的视图没有参考顶部的视图模型,类似这样的视图 @model Nameofyourproject.Models.CalibrationCommentViewModel

      【讨论】:

      • 我说那是我观点的一部分。我当然在顶部包含了我的强类型模型。
      猜你喜欢
      • 2015-10-29
      • 2013-07-16
      • 2020-01-20
      • 1970-01-01
      • 2013-02-03
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多