8

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

我正在使用 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(同样,只显示一个输入;nb:使用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("some-email@example.com");
        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 代码放在哪里,但我想我可能是对的):

公共类报价控制器:控制器{

    /// <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 视图。

我感谢社区的帮助。

4

1 回答 1

15

您可以让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("some-email@example.com");

        // 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>
于 2011-07-10T17:19:39.803 回答