所以这是我在控制器中的代码:
[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult Index(ContactModel c)
{
//make sure the data is valid
if (ModelState.IsValid)
{
MailMessage msg = new MailMessage(
"dummyemail",//from email
"dummyemail",//to email
"Message from your site-" + c.Subject,//subject of email
string.Format("Name:{0}<br/>Desired Method of Contact:{1}<br/>Contact Info:{2}<br/>Message:{3}", c.Name, c.ContactInfo, c.Address, c.Message)
);//end mailmessage msg
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;
//sends the email
using(SmtpClient client = new SmtpClient("smtp.secureserver.net"))
{
client.Send(msg);
}//end using statement to avoid opening and closing the connection strings
return RedirectToAction("Index");
}//end if modelstate is valid
return View(c);
这就是我在页面上的内容:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<h4>Contact me by E-Mail</h4>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.ContactInfo, "Phone Number", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ContactInfo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ContactInfo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address, "Address", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ContactInfo, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.Subject, "Type of Service", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Subject, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Subject, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.Message, "Comments", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Send" class="btn btn-default" />
</div>
</div>
}@*end using(htmlbeginform())*@
这是我的模特:
public class ContactModel
{
[Required(ErrorMessage = "Please enter your name")]
public string Name { get; set; }//this will be the customer's name.
[Required(ErrorMessage = "Please enter your phone")]
[DisplayName("Phone")]
public string ContactInfo { get; set; }//this will be either an email or a phone number.
[Required(ErrorMessage = "Please enter an address")]
[DisplayName("Address")]
public string Address { get; set; }
public string Subject { get; set; }//can be left blank, we'll fill it in anyways.
[Required(ErrorMessage = "Please enter your message")]
[UIHint("MultiLineText")]//used with the .editorfor on the view - - - causes the textbox to be a textarea instead
public string Message { get; set; }
}//end class
对于我的生活,我无法弄清楚为什么当电子邮件被发送时,它们包含除了消息部分之外的所有内容。多线,客户会告诉我们他们需要什么的重要部分。 OP非常感谢协助。