ASP MVC上传帮助扩展问题

时间:2011-11-09 19:22:42

标签: asp.net-mvc file-upload html-helper

我遇到了以下问题: 我有这个模型

public class Region
{
    public int RegionID { get; set; }
    public string RegionName { get; set; }
    public HttpPostedFile CustomFile { get; set; }
}

我有here的自定义扩展html帮助方法。

现在在视图中我有以下代码:

<%using (Html.BeginForm("ModifyRegion", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))

{%GT;

  <%: Html.TextAreaFor(x => x.RegionID, Model.RegionID)%>
  <br />
  <%: Html.TextAreaFor(x => x.RegionName, Model.RegionName)%>
  <br />

 <%: Html.FileBoxFor(x=>x.CustomFile, Model.CustomFile) %>

<input type="submit" />
 

当它到达我的控制器操作时,CustomFile字段为空,但所有其他字段都已正确设置。如果我将CustomFile属性更改为字符串而不是HttpPostedFile,我会正确获取文件名(例如“dog.jpg”)。有没有办法正确获取完整的HttpPostedFile文件?

提前致谢, Tamash

2 个答案:

答案 0 :(得分:1)

尝试在视图模型中将HttpPostedFile替换为HttpPostedFileBase

public class Region
{
    public int RegionID { get; set; }
    public string RegionName { get; set; }
    public HttpPostedFileBase CustomFile { get; set; }
}

还要确保您使用的自定义FileBoxFor帮助程序生成相应文件字段的正确名称和类型:

<input name="CustomFile" type="file" />

答案 1 :(得分:0)

您可以使用以下内容:

&LT; input name =“CustomFile”type =“file”value =“Browse”/&gt;

相关问题