【发布时间】: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