MVC 4 の連絡先フォームを変更して、ユーザーにメール受信者の 2 つの異なる選択肢を与える必要があります。当初は情報とコメントのどちらかを選択できましたが、送信されたコメントを受信する受信者 (1. リゾートまたは 2. カジノ) を選択するだけで済みます。選択を変更し始めましたが、モデルとコントローラーを台無しにする前に確認したかったのです。
見る - お問い合わせフォーム
<div id="ContactUsFormContainer">
@using (Html.BeginForm())
{
@Html.ValidationSummary()
@Html.DropDownListFor(x => x.ContactRecipient, new[]
{
new SelectListItem() {Value = "Resort Comment"},
new SelectListItem() {Value = "Casino Comment"}})<br />
<div class="inputSmBox">
*Your First Name<br />
@Html.TextBoxFor(x => x.FirstName, new { @style = "width: 180px;", @class = "fHandleClass" })
</div>
<div class="inputSmBox">
*Your Last Name:<br />
@Html.TextBoxFor(x => x.LastName, new { @style = "width: 180px;" })
</div>
<div class="inputSmBox">
*Your Phone Number:<br />
@Html.TextBoxFor(x => x.Phone, new { @style = "width: 180px;" })
</div>
<div class="inputSmBox">
*Your Email Address:<br />
@Html.TextBoxFor(x => x.Email, new { @style = "width: 180px;" })
</div>
<p class="inputSmBox">
*Subject<br />
@Html.TextBoxFor(x => x.Subject, new { @style = "width: 400px;" })
</p>
<p class="inputSmBox" style="margin-top: 5px;">
*Type your comments below<br />
@Html.EditorFor(x => x.Comment, new { @style = "width: 400px;" })
</p>
<div class="Clear"></div>
<p>
<input type="submit" value="" class="submitButton" id="submitButtonStyle" />
</p>
}
</div>
見る - ありがとう
@{
try {
WebMail.SmtpServer = "mail.resortdata.com";
WebMail.From = "no-reply@casino.com";
WebMail.Send("info@resort.com", "From Resort Casino Website Contact Us Form",
Model.FirstName + " " + Model.LastName + " has initiated contact with Resort Casino via the website contact form." + "<br /><br />" + "Phone number:" + " " + Model.Phone + "<br /><br />" + "Email address:" + " " + Model.Email + "<br /><br />" + "Subject:" + " " + Model.Subject + "<br /><br />" + "<b>Comment:</b>" + "<br />" + Model.Comment +"<br />");
}
catch (Exception)
{
@:<b>Sorry we could not send an email with your contact information.</b>
}
}
<h2>Thank you @Model.FirstName for your comment.
</h2>
コントローラ
public ViewResult ContactUsThankYou()
{
int hour = DateTime.Now.Hour;
ViewData["greeting"] = hour < 12 ? "Good Morning!" : "Good Afternoon";
return View();
}
[HttpGet]
public ViewResult ContactUs()
{
return View();
}
[HttpPost]
public ViewResult ContactUs(ContactUs ContactUs)
{
if (ModelState.IsValid)
{
return View("ContactUsThankYou", ContactUs);
}
else
{
return View();
}
}
モデル
public class ContactUs
{
[Required(ErrorMessage = "Please enter your first name")]
[RegularExpression("^[a-zA-Z]+$",
ErrorMessage = "Please enter valid characters")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please enter your last name")]
[RegularExpression("^[a-zA-Z]+$",
ErrorMessage = "Please enter valid characters")]
public string LastName { get; set; }
[Required(ErrorMessage = "Please enter your phone #")]
[RegularExpression(@"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}",
ErrorMessage = "Please enter a valid phone number 555-555-5555")]
public string Phone { get; set; }
[Required(ErrorMessage = "Please enter your email")]
[RegularExpression("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?",
ErrorMessage = "Please enter a valid email address all lower case")]
public string Email { get; set; }
[Required(ErrorMessage = "Please enter a subject")]
//[RegularExpression("^[a-zA-Z]+$",
// ErrorMessage = "Please enter a subject without odd characters")]
public string Subject { get; set; }
[Required(ErrorMessage = "Please enter your request or statement in the comment box")]
[RegularExpression("^[a-zA-Z]+$",
ErrorMessage = "Please enter valid characters")]
[DataType(DataType.MultilineText)]
public string Comment { get; set; }
}
ありがとう