【问题标题】:Dont get an attachment with email asp.net mvc 4 Model.ProjectInformation = null不要通过电子邮件获取附件 asp.net mvc 4 Model.ProjectInformation = null
【发布时间】:2013-05-23 22:20:28
【问题描述】:

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

  @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

但我不明白为什么?

【问题讨论】:

  • 检查HERE
  • 但我的控制器中有[HttpPost] public ActionResult Feedback(FeedbackForm Model) 如何在我的操作方法中添加HTTPPOST 属性?
  • 我添加了@using (Html.BeginForm("Feedback", "Home", FormMethod.Post, new { id = "feedback-form", enctype = "multipart/form-data" })) 和public ActionResult Feedback(FeedbackForm Model, HttpPostedFileBase ProjectInformation) 但我收到错误i.imgur.com/uQhMvtA.png 怎么了?
  • ProjectInformation 是FeedbackForm 类的属性?如果是,那么您不需要动作方法的第二个参数(HttpPostedFileBase ProjectInformation)。文件已绑定到您的 model.ProjectInformation

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


【解决方案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);
        }
    }
}

【讨论】:

  • 不,我再次进入HttpPostedFileBase hpf = Model.ProjectInformation; hpf = null,我不明白为什么!
  • 当我尝试添加附件时,我根本没有收到电子邮件。
  • 尝试在 if 块中直接使用 Model.ProjectInformation。我的意思是if(Model.ProjectInformation.ContentLength &gt; 0)
  • 看来我需要在这里添加第二个参数public ActionResult Feedback(FeedbackForm Model, HttpPostedFileBase ProjectInformation),如果我不添加它我不会收到电子邮件,但是使用这个参数它可以工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-11
  • 2014-10-07
  • 2021-12-24
  • 1970-01-01
相关资源
最近更新 更多