我有MVC3 project
。在我的联系页面中,我想添加为客户发送的电子邮件添加附件的可能性。
我正在使用Postal
。它运作良好但我无法使附件工作。
以下是我处理此代码的部分:
我有model class
:
public class ContactEmail
{
[ScaffoldColumn(false)]
public int id { get; set; }
[Required(ErrorMessage = "Name is required!")]
[DataType(DataType.Text)]
[DisplayName("Name")]
public string name { get; set; }
[Required(ErrorMessage = "Email Address is required!")]
[DataType(DataType.EmailAddress)]
[DisplayName("Email Address")]
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}", ErrorMessage = "Email Address is not valid.")]
public string email { get; set; }
[Required(ErrorMessage = "Country is required!")]
[DisplayName("Country")]
public string country { get; set; }
[DisplayName("Subject")]
public string subject { get; set; }
[Required(ErrorMessage = "Message is required!")]
[DisplayName("Message")]
[StringLength(1500)]
public string message { get; set; }
[ScaffoldColumn(false)]
public DateTime datetime { get; set; }
public HttpPostedFileBase attachment { get; set; }
这是我的Postal Controller
:
public class PostalController : Controller
{
[HttpPost]
public ActionResult Send(ContactEmail model)
{
if (ModelState.IsValid)
{
Random random = new Random();
var ticket = random.Next(1000000000, 2000000000);
dynamic courriel = new Email("Postal");
courriel.To = "support@coderfortraders.com";
courriel.Name = model.name;
courriel.Country = model.country;
courriel.Subject = model.subject;
courriel.From = model.email;
courriel.Message = model.message;
courriel.TicketId = ticket;
courriel.Attachment = model.attachment;
//if (model.Attachment != null && model.Attachment.ContentLength > 0)
//{
// var attachment = new Attachment(model.Attachment.InputStream, model.Attachment.FileName);
// courriel.Attachments.Add(attachment);
//}
//courriel.Attachment = model.Attachment;
courriel.Attach(new Attachment(model.attachment.InputStream, model.attachment.FileName));
Task task = courriel.SendAsync();
//courriel.Send();
return RedirectToAction("Sent", "Contact");
}
ViewData["MessageSent"] = false;
return View("~/Views/Contact/Contact.cshtml");
}
最后这是我在Contact.cshtml view
中使用的表单:
@using (Html.BeginForm("Send", "Postal"))
{
@Html.ValidationSummary(false)
<p>
@Html.LabelFor(model => model.name)
@Html.TextBoxFor(model => model.name, new { @size = 35 })
@Html.ValidationMessageFor(model => model.name, "*")
</p>
<p>
@Html.LabelFor(model => model.email)
@Html.TextBoxFor(model => model.email, new { @size = 35 })
@Html.ValidationMessageFor(model => model.email, "*")
</p>
<p>
@Html.LabelFor(model => model.subject)
@Html.DropDownList(
"subject",
new[] {
new SelectListItem {
Text = "NT Indicator Quote",
Value = "NT Indicator Quote"
},
new SelectListItem {
Text = "NT Strategy Quote",
Value = "NT Strategy Quote"
},
new SelectListItem {
Text = "Comments",
Value = "Comments"
},
new SelectListItem {
Text = "other",
Value = "other"
}
}
); // @Html.DropDownList(
@Html.ValidationMessageFor(model => model.subject, "*")
</p>
<p>
@Html.LabelFor(model => model.message)
@Html.TextAreaFor(model => model.message, new { @cols = 50, @rows = 10 })
@Html.ValidationMessageFor(model => model.message, "*")
</p>
<p>
@Html.LabelFor(model => model.attachment)
@Html.TextBoxFor(model => model.attachment, new { type = "file" })
@Html.ValidationMessageFor(model => model.attachment)
</p>
<p><input type="submit" id="submit" name="submit" value="Submit" /></p>
} <!-- End form -->
我收到以下错误消息:
对象引用未设置为对象的实例。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中的起源位置的更多信息 异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例 来源错误: 第44行:courriel.Attach(新附件(model.attachment.InputStream,model.attachment.FileName));
你能帮我解决一下这个问题吗? 谢谢!
以下是来自视图源的HTML:
<form action="/Postal/Send?enctype=multipart%2Fform-data" method="post">
<div class="validation-summary-valid" data-valmsg-summary="true">
<ul>
<li style="display:none"></li>
</ul>
</div>
<p>
<label for="name">Name</label>
<input data-val="true" data-val-required="Name is required!" id="name" name="name" size="35" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="name" data-valmsg-replace="false">*</span>
</p>
<p>
<label for="email">Email Address</label>
<input data-val="true" data-val-regex="Email Address is not valid." data-val-regex-pattern="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}" data-val-required="Email Address is required!" id="email" name="email" size="35" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="email" data-valmsg-replace="false">*</span>
</p>
<p>
<label for="subject">Subject</label>
<select id="subject" name="subject">
<option value="NT Indicator Quote">NT Indicator Quote</option>
<option value="NT Strategy Quote">NT Strategy Quote</option>
<option value="Comments">Comments</option>
<option value="other">other</option>
</select>
<span class="field-validation-valid" data-valmsg-for="subject" data-valmsg-replace="false">*</span>
</p>
<p>
<label for="message">Message</label>
<textarea cols="50" data-val="true" data-val-length="The field Message must be a string with a maximum length of 1500." data-val-length-max="1500" data-val-required="Message is required!" id="message" name="message" rows="10">
</textarea>
<span class="field-validation-valid" data-valmsg-for="message" data-valmsg-replace="false">*</span>
</p>
<p>
<label for="attachment">attachment</label>
<input id="attachment" name="attachment" type="file" value="" />
<span class="field-validation-valid" data-valmsg-for="attachment" data-valmsg-replace="true"></span>
</p>
<p>
<input type="submit" id="submit" name="submit" value="Submit" />
</p>
</form> <!-- End form -->
</div>
答案 0 :(得分:3)
如果要上传文件,则需要将enctype="multipart/form-data"
属性附加到表单:
@using (Html.BeginForm("Send", "Postal", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
...
}
另外由Haacked查看此blog post。