MvcMailer:通过模型在视图中获取用户输入,然后将其插入到邮件中

发布于 2024-11-19 02:36:14 字数 4133 浏览 7 评论 0原文

我不知道我是否正确解释了这一点,或者解决方案是否相当简单,所以这里是:

我正在使用 MvcMailer,但在此之前我设置了一个向导输入表单,我将其称为 Quote.cshtml。在 Quote.cshtml 后面,我设置了一个名为 QuoteModel.cs 的模型。

Quote.cshtml 最基本的部分(我省略了所有向导逻辑,只显示一个输入):

<td width="40%">
    @Html.LabelFor(m => m.FirstName, new { @class = "mylabelstyle", title = "Enter first name." })
</td>
<td width="60%">
    @Html.TextBoxFor(m => m.FirstName)
    @Html.ValidationMessageFor(m => m.FirstName)
</td>

QuoteModel.cs(同样,只显示一个输入;注意:使用 DataAnnotationExtensions

public class QuoteModel
{ 
    [Required(ErrorMessage = "First Name required.")]
    [Display(Name = "First Name:")]
    public string FirstName { get; set; }
}

现在我正在尝试集成 MvcMailer,它设置了 IQuoteMailer.cs、QuoteMailer.cs、 _Layout.cshtml 和 QuoteMail.cshtml。 QuoteMail.cshtml 是邮件收件人最终将看到的内容。我还设置了一个 QuoteController.cs,在其中放置了 MvcMailer 所需的适当代码。在 QuoteMailer.cs 和 QuoteController.cs 中,我无法传递来自 Quote.cshtml 的用户输入(基于 QuoteModel.cs 中的模型)。

IQuoteMailer.cs:

 public interface IQuoteMailer
    {               
         MailMessage QuoteMail();
    }

QuoteMailer.cs:

public class QuoteMailer : MailerBase, IQuoteMailer     
{
    public QuoteMailer():
        base()
    {
        MasterName="_Layout";
    }


    public virtual MailMessage QuoteMail()
    {
        var mailMessage = new MailMessage{Subject = "QuoteMail"};

        mailMessage.To.Add("[email protected]");
        ViewBag.Data = someObject; 
                    //I imagine this is where I can pass my model, 
                    //but I am not sure (do I have to iterate each and
                    //every input (there are like 20 in QuoteModel.cs)?

                return mailMessage;
    }

QuoteMail.cshtml (_Layout.cshtml 非常标准,因此此处未显示):

@*HTML View for QuoteMailer#QuoteMail*@

Welcome to MvcMailer and enjoy your time!<br />
<div class="mailer_entry">
    <div class="mailer_entry_box">
        <div class="mailer_entry_text">
            <h2>
                INSERT_TITLE
            </h2>
            <p>
                INSERT_CONTENT
                //I believe I am going to use a "@" command like @ViewData
                //to pass FirstName, but again, not sure how to bind 
                //the model and then pass it.
            </p>
            <p>
                INSERT_CONTENT
            </p>
        </div>
    </div>
</div>

最后是 QuoteController.cs 的相关部分(请注意,我正在使用向导,因此,我的问题是弄清楚在哪里放置 MvcMailer 代码,但我想我可能是对的):

public class QuoteController: Controller 因此

    /// <summary>
    /// MvcMailer
    /// </summary>
    private IQuoteMailer _quoteMailer = new QuoteMailer();
    public IQuoteMailer QuoteMailer
    {
        get { return _quoteMailer; }
        set { _quoteMailer = value; }
    }

    //
    // GET: /Quote/
    [HttpGet]
    public ActionResult Quote()
    {
        HtmlHelper.ClientValidationEnabled = true;
        HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
        //In order to get the clientside validation (unobtrusive), 
        //the above lines are necessary (where action takes place)
        return View();
    }

    //
    // POST: /Matrimonial/
    [HttpPost]
    public ActionResult Quote(QuoteModel FinishedQuote)
    {
        if (ModelState.IsValid)
        {
            QuoteMailer.QuoteMail().Send();
            return View("QuoteMailSuccess", FinishedQuote);
        }
        else return View();
    }

    //
    // POST: /Matrimonial/Confirm
    [HttpPost]
    public ActionResult QuoteMailConfirm(QuoteModel FinishedQuote)
    {
        return PartialView(FinishedQuote);
    }

我的困惑是如何传递我创建的 QuoteModel,以便最终我可以获取用户输入的数据,然后生成 MvcMailer 视图。

我感谢社区的帮助。

I don't know if I am explaining this correctly, or if the solution is rather simple, so here goes:

I am using MvcMailer, but before that I set up a wizard input form which I call Quote.cshtml. Behind Quote.cshtml, I set up a model called QuoteModel.cs.

Quote.cshtml at its most basic (I am leaving out all of the wizard logic and only showing one input):

<td width="40%">
    @Html.LabelFor(m => m.FirstName, new { @class = "mylabelstyle", title = "Enter first name." })
</td>
<td width="60%">
    @Html.TextBoxFor(m => m.FirstName)
    @Html.ValidationMessageFor(m => m.FirstName)
</td>

QuoteModel.cs (again, only showing the one input; n.b.: using the DataAnnotationExtensions)

public class QuoteModel
{ 
    [Required(ErrorMessage = "First Name required.")]
    [Display(Name = "First Name:")]
    public string FirstName { get; set; }
}

Now I am trying to integrate MvcMailer, which sets up IQuoteMailer.cs, QuoteMailer.cs, _Layout.cshtml, and QuoteMail.cshtml. The QuoteMail.cshtml is what the recipient of the mail will eventually see. I also set up a QuoteController.cs, in which I placed the appropriate code required by MvcMailer. It is in the QuoteMailer.cs and QuoteController.cs where I am having trouble passing the user input from Quote.cshtml (which is based on the model in QuoteModel.cs).

IQuoteMailer.cs:

 public interface IQuoteMailer
    {               
         MailMessage QuoteMail();
    }

QuoteMailer.cs:

public class QuoteMailer : MailerBase, IQuoteMailer     
{
    public QuoteMailer():
        base()
    {
        MasterName="_Layout";
    }


    public virtual MailMessage QuoteMail()
    {
        var mailMessage = new MailMessage{Subject = "QuoteMail"};

        mailMessage.To.Add("[email protected]");
        ViewBag.Data = someObject; 
                    //I imagine this is where I can pass my model, 
                    //but I am not sure (do I have to iterate each and
                    //every input (there are like 20 in QuoteModel.cs)?

                return mailMessage;
    }

QuoteMail.cshtml (_Layout.cshtml is pretty standard, so not showing here):

@*HTML View for QuoteMailer#QuoteMail*@

Welcome to MvcMailer and enjoy your time!<br />
<div class="mailer_entry">
    <div class="mailer_entry_box">
        <div class="mailer_entry_text">
            <h2>
                INSERT_TITLE
            </h2>
            <p>
                INSERT_CONTENT
                //I believe I am going to use a "@" command like @ViewData
                //to pass FirstName, but again, not sure how to bind 
                //the model and then pass it.
            </p>
            <p>
                INSERT_CONTENT
            </p>
        </div>
    </div>
</div>

And finally, the relevant parts of the QuoteController.cs (note that I have am using a wizard, therefore, part of my problem is figuring out where to put the MvcMailer code, but I think I may have it right):

public class QuoteController: Controller
{

    /// <summary>
    /// MvcMailer
    /// </summary>
    private IQuoteMailer _quoteMailer = new QuoteMailer();
    public IQuoteMailer QuoteMailer
    {
        get { return _quoteMailer; }
        set { _quoteMailer = value; }
    }

    //
    // GET: /Quote/
    [HttpGet]
    public ActionResult Quote()
    {
        HtmlHelper.ClientValidationEnabled = true;
        HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
        //In order to get the clientside validation (unobtrusive), 
        //the above lines are necessary (where action takes place)
        return View();
    }

    //
    // POST: /Matrimonial/
    [HttpPost]
    public ActionResult Quote(QuoteModel FinishedQuote)
    {
        if (ModelState.IsValid)
        {
            QuoteMailer.QuoteMail().Send();
            return View("QuoteMailSuccess", FinishedQuote);
        }
        else return View();
    }

    //
    // POST: /Matrimonial/Confirm
    [HttpPost]
    public ActionResult QuoteMailConfirm(QuoteModel FinishedQuote)
    {
        return PartialView(FinishedQuote);
    }

}

So, my confusion is to how to pass the QuoteModel I created, so that ultimately I can take the user inputed data and then generate the MvcMailer view.

I appreciate the communities help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东北女汉子 2024-11-26 02:36:14

您可以让 IQuoteMailer 接口采用模型:

public interface IQuoteMailer
{
    MailMessage QuoteMail(QuoteModel model);
}

并在实现中使用此模型:

public class QuoteMailer : MailerBase, IQuoteMailer
{
    public QuoteMailer() : base()
    {
        MasterName = "_Layout";
    }


    public virtual MailMessage QuoteMail(QuoteModel model)
    {
        var mailMessage = new MailMessage
        {
            Subject = "QuoteMail"
        };
        mailMessage.To.Add("[email protected]");

        // Use a strongly typed model
        ViewData = new ViewDataDictionary(model);
        PopulateBody(mailMessage, "QuoteMail", null);
        return mailMessage;
    }
}

然后当您决定发送邮件时从控制器传递模型:

[HttpPost]
public ActionResult Quote(QuoteModel FinishedQuote)
{
    if (ModelState.IsValid)
    {
        QuoteMailer.QuoteMail(FinishedQuote).Send();
        return View("QuoteMailSuccess", FinishedQuote);
    }
    else return View();
}

最后在模板中(〜 /Views/QuoteMailer/QuoteMail.cshtml)您可以使用该模型:

@using AppName.Models
@model QuoteModel

Welcome to MvcMailer and enjoy your time!
<br />
<div class="mailer_entry">
    <div class="mailer_entry_box">
        <div class="mailer_entry_text">
            <h2>
                INSERT_TITLE
            </h2>
            <p>
                Hello @Model.FirstName
            </p>
            <p>
                INSERT_CONTENT
            </p>
        </div>
    </div>
</div>

You could have the IQuoteMailer interface take the model:

public interface IQuoteMailer
{
    MailMessage QuoteMail(QuoteModel model);
}

and in the implementation use this model:

public class QuoteMailer : MailerBase, IQuoteMailer
{
    public QuoteMailer() : base()
    {
        MasterName = "_Layout";
    }


    public virtual MailMessage QuoteMail(QuoteModel model)
    {
        var mailMessage = new MailMessage
        {
            Subject = "QuoteMail"
        };
        mailMessage.To.Add("[email protected]");

        // Use a strongly typed model
        ViewData = new ViewDataDictionary(model);
        PopulateBody(mailMessage, "QuoteMail", null);
        return mailMessage;
    }
}

then from the controller when you decide to send the mail pass the model:

[HttpPost]
public ActionResult Quote(QuoteModel FinishedQuote)
{
    if (ModelState.IsValid)
    {
        QuoteMailer.QuoteMail(FinishedQuote).Send();
        return View("QuoteMailSuccess", FinishedQuote);
    }
    else return View();
}

and finally in the template (~/Views/QuoteMailer/QuoteMail.cshtml) you could use the model:

@using AppName.Models
@model QuoteModel

Welcome to MvcMailer and enjoy your time!
<br />
<div class="mailer_entry">
    <div class="mailer_entry_box">
        <div class="mailer_entry_text">
            <h2>
                INSERT_TITLE
            </h2>
            <p>
                Hello @Model.FirstName
            </p>
            <p>
                INSERT_CONTENT
            </p>
        </div>
    </div>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文