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