不要通过电子邮件获得附件asp.net mvc 4 Model.ProjectInformation = null

时间:2013-05-23 13:45:09

标签: asp.net-mvc asp.net-mvc-4

我的网站上有反馈表单,我需要在电子邮件中添加附件 我有

  @Html.TextBoxFor(model => model.ProjectInformation, null, new { type = "file", @class = "input-file" })

=

<input type="file" id="ProjectInformation">

我在控制器中写了那段代码

        //Attachment
        if (Model.ProjectInformation != null && !(String.IsNullOrEmpty(Model.ProjectInformation.FileName)))
        {
            HttpPostedFileBase hpf = this.Request.Files[Model.ProjectInformation.FileName];
            if (hpf.ContentLength > 0)
            {
                var attach = new Attachment(hpf.InputStream, Model.ProjectInformation.FileName);
                msg.Attachments.Add(attach);
            }
        }

但是我收到了没有附件的电子邮件,当我检查调试器时,我看到了   Model.ProjectInformation = null

但我不明白为什么?

1 个答案:

答案 0 :(得分:1)

Model.ProjectInformation已经是HttpPostedFileBase来自您的观点,您无需尝试从请求中获取文件。

[HttpPost]
public ActionResult Feedback(FeedbackForm Model)
{
    //Attachment
    if (Model.ProjectInformation != null && !(String.IsNullOrEmpty(Model.ProjectInformation.FileName)))
    {
        HttpPostedFileBase hpf = Model.ProjectInformation;
        if (hpf.ContentLength > 0)
        {
            var attach = new Attachment(hpf.InputStream, Model.ProjectInformation.FileName);
            msg.Attachments.Add(attach);
        }
    }
}