与angular和asp mvc有点不同的东西 - 来自modal的电子邮件

时间:2014-04-28 17:24:58

标签: asp.net-mvc angularjs

我正在尝试做一些不同的事情,所以我希望你和我一起工作。我的目标是在asp mvc应用程序中使用有角度的单页应用程序。我有我的理由,但一个是我喜欢两者的一些好处。所以我的问题是这个。是否可以在角度模态中使用webmail助手。

以下是我的一些代码。我觉得我很接近,但现在我不确定它是如何与我的控制器进行交互的。

电子邮件控制器 - ASP

public class EmailController : Controller

{
    //
    // GET: /Home/

public ViewResult Index()
{
    int hour = DateTime.Now.Hour;
    ViewBag.Greeting = hour < 12 ? "Good Morning" : "Good Afternoon";
    return View();
}

[HttpGet]
public ViewResult RequestForm()
{
    return View();
}

[HttpPost]
public ViewResult RequestForm(EmailRequest emailrequest)
{
    if (ModelState.IsValid)
    {
        // TODO: Email response to the party organizer 
        return View("Thanks", emailrequest);
    }
    else
    {
        // there is a validation error
        return View();
    }

}

角度模态

<div class="modal" ng-show="isEmailPopupVisible">
    <div class="modal-header">
        <button type="button" class="close" ng-click="closeEmailPopup()">×</button>
    </div>
    <div class="modal-body">
        @using (Html.BeginForm())
        {
            @Html.ValidationSummary(true)
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "text", placeholder = "Enter your Name" }, })
            <br />
            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "text", placeholder = "Enter Your Email" }, })
            @Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "text", placeholder = "Enter Phone Number" }, })
            <p>
                Will you attend?
                @Html.DropDownListFor(x => x.WillAttend, new[] {
                new SelectListItem() {Text = "Yes, I'll be there",
                Value = bool.TrueString},
                new SelectListItem() {Text = "No, I can't come",
                Value = bool.FalseString}
                }, "Choose an option")
            </p>
            <input type="submit" class="btn btn-primary" value="Send Email" />
        }
    </div>
    <div class="modal-footer">

          @* Issue with submit, not sure how to register thanks page and webmail logic.  *@

            <a href="#" class="btn btn" ng-click="closeEmailPopup()">Close</a>
    </div>
</div>

ASP模型

public class EmailRequest
{

    [Required(ErrorMessage = "Please enter your name")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Please enter your email address")]
    [RegularExpression(".+\\@.+\\..+",
        ErrorMessage = "Please enter a valid email address")]
    public string Email { get; set; }

    [Required(ErrorMessage = "Please enter your phone number")]
    public string Phone { get; set; }

    [Required(ErrorMessage = "Please specify whether you'll attend")]
    public bool? WillAttend { get; set; }
}

由于-CSHTML

@model Project.Models.EmailRequest

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Thanks</title>
</head>
<body>
    @{
        try
        {
            WebMail.SmtpServer = "myserver";
            WebMail.SmtpPort = myport;
            WebMail.EnableSsl = false;
            WebMail.UserName = "myemail";
            WebMail.Password = "mypass";
            WebMail.From = "myemail";

            WebMail.Send("myemail", "Request Notification",
                Model.Name + " is " + ((Model.WillAttend ?? false) ? "" : "not")
                + "attending");
        }
        catch (Exception)
        {
            @:<b>Sorry - we couldn't send the email to confirm your RSVP. </b>
    }
    }
    <div>
        <h1>Thank you, @Model.Name!</h1>
        @if (Model.WillAttend == true)
        {
            @:I'ts great that you're coming.  The drinks are already in the fridge!
      }
        else
        {
            @:Sorry to hear that you can't make it, but thanks for letting us know.
      }
    </div>
</body>
</html>

我目前在“thanks.cshtml”页面中有webmail smtp设置等,但这似乎与使用控制器的标准asp mvc方式有关。我真的不知道我是否已经向你展示了我的目标,但有没有办法从模态提交到我的webmail设置而不包括模态中的设置(用户可以看到源代码)。 / p>

这是一个可怕的想法,还是我对某些有希望的事情有点接近。

1 个答案:

答案 0 :(得分:0)

我认为WebMail中的Thanks.cshtml代码应该在您的控制器中。有点难以说出你的问题是什么,你希望将来如何发生流程,你是否希望“谢谢”出现在表格所在的同一模态中?我会发一个AJAX调用来发送邮件并返回成功,它甚至可以从你可以在你的模态中替换的视图中返回HTML,如果你愿意...