【发布时间】:2010-11-26 09:09:02
【问题描述】:
我的数据库中有一个 image 类型的字段,它在我的模型(ADO.NET 实体框架)中映射为二进制。
但是不知何故,我从输入文件框中得到的图像没有被传递给对象。我知道这一点是因为我调试了我的操作并且对象语言(我尝试上传到数据库的图像是一个标志)的属性标志设置为 null,这非常糟糕!它应该包含上传的图像。我还需要做点别的吗?
以下是我的表单 html 代码和我的操作代码:
<% using (Html.BeginForm("Create", "Language", FormMethod.Post,
new {enctype="multipart/form-data"})) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Id">Id:</label>
<%= Html.TextBox("Id") %>
<%= Html.ValidationMessage("Id", "*") %>
</p>
<p>
<label for="Name">Name:</label>
<%= Html.TextBox("Name") %>
<%= Html.ValidationMessage("Name", "*") %>
</p>
<p>
<label for="Flag">Flag:</label>
<!--
File box is a helper that I got from this link:
http://pupeno.com/blog/file-input-type-for-forms-in-for-asp-net-mvc/
-->
<%= Html.FileBox("Flag") %>
<%= Html.ValidationMessage("Flag", "*") %>
</p>
<p>
<label for="IsDefault">IsDefault:</label>
<%= Html.TextBox("IsDefault") %>
<%= Html.ValidationMessage("IsDefault", "*") %>
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
我正在使用 Visual Studio 2008。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Language language)
{
// Here language.Flag is null. Shouldn't the Flag property be a
// binary field ready to be stored in the database along with the others?
if (!ModelState.IsValid || !_service.CreateLanguage(language))
{
return View("Create", language);
}
return RedirectToAction("Index");
}
我做错了什么?
【问题讨论】: