我的网站上有反馈表,看起来像
我为我的表格创建了模型
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace CorePartners_Site2.Models
{
public class FeedbackForm
{
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Company { get; set; }
public string AdditionalInformation { get; set; }
[FileExtensions(Extensions = "doc,txt,pdf")]
public HttpPostedFileBase ProjectInformation { get; set; }
}
}
并创建了视图
@using (Html.BeginForm("Feedback", "Home", FormMethod.Post, new { id = "feedback-form" }))
{
@Html.TextBoxFor(model => model.Name, null, new { @class = "text-field" })
@Html.TextBoxFor(model => model.Email, null, new { @class = "text-field" })
@Html.TextBoxFor(model => model.Phone, null, new { @class = "text-field" })
@Html.TextBoxFor(model => model.Company, null, new { @class = "text-field" })
@Html.TextAreaFor(model => model.AdditionalInformation, new { cols="1", rows="1" })
@Html.TextBoxFor(model => model.ProjectInformation, null, new { type="file", @class="input-file" })
<a href="#" class="link1" onclick="document.getElementById('feedback-form').submit()"><em><b>Send</b></em></a>
}
我想知道我的表单是否可以a
用于提交<input type="submit" />
?
而且我不知道如何制作附件,我试着制作
[HttpGet]
public ActionResult Feedback()
{
return View();
}
[HttpPost]
public ActionResult Feedback(FeedbackForm Model)
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.BodyEncoding = Encoding.UTF8;
msg.From = new MailAddress(Model.Email, @Resources.Global.Feedback_Email_Title);
msg.To.Add("tayna-anita@mail.ru");
string message = @Resources.Global.Feedback_Name + ": " + Model.Name + "\n"
+ @Resources.Global.Feedback_Email + ": " + Model.Email + "\n"
+ @Resources.Global.Feedback_Phone + ": " + Model.Phone + "\n"
+ @Resources.Global.Feedback_Company + ": " + Model.Company + "\n\n"
+ Model.AdditionalInformation;
msg.Body = message;
msg.IsBodyHtml = false;
//Attachment
if (Model.ProjectInformation != null)
{
HttpPostedFileBase attFile = Model.ProjectInformation;
int attachFileLength = attFile.ContentLength;
if (attachFileLength > 0)
{
string strFileName = Path.GetFileName(Model.ProjectInformation.FileName);
Model.ProjectInformation.SaveAs(Server.MapPath(strFileName));
MailAttachment attach = new MailAttachment(Server.MapPath(strFileName));
msg.Attachments.Add(attach);
string attach1 = strFileName;
}
}
SmtpClient client = new SmtpClient("smtp.mail.ru", 25);
client.UseDefaultCredentials = false;
client.EnableSsl = false;
try
{
client.Send(msg);
}
catch (Exception ex)
{
}
FeedbackForm tempForm = new FeedbackForm();
return View(tempForm);
}
但它显示了一个错误,msg.Attachments.Add(attach);
似乎它不起作用。