0

我需要更改 MVC 4 中的联系表单,以便为用户提供两种不同的电子邮件收件人选择。最初我可以在信息和评论之间进行选择,现在我只需要在收件人(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; }

    }

谢谢

4

1 回答 1

0

假设您的表单和设置有效,那么我认为您所要做的就是在 Model.ContactRecipient 值上设置一个条件来投影 Webmail.To 值。我尚未运行此解决方案,但请在下面尝试。

 @{
try {

   WebMail.SmtpServer = "mail.resortdata.com";

    WebMail.From = "no-reply@casino.com";

 if(Model.ContactRecipient="Resort Comment"){
  Webmail.To = "info@resort.com"
   }
  else
   {
  Webmail.To = "info@casino.com"
    }
    WebMail.Send(Webmail.To, "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>
于 2013-09-10T22:46:50.507 回答