【问题标题】:MvcMailer: Taking user input in view through model and then inserting it into mailMvcMailer:通过模型在视图中获取用户输入,然后将其插入邮件
【发布时间】:2011-10-02 06:14:28
【问题描述】:

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

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

公共类 QuoteController: 控制器 {

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

感谢社区的帮助。

【问题讨论】:

    标签: asp.net-mvc view model mvcmailer


    【解决方案1】:

    您可以让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>
    

    【讨论】:

    • 出去走走。谢谢回答。稍后会检查并报告进度。
    • 这很好用,但我真的希望 PopulateBody 函数有一个模型参数。我想我可以很容易地添加一个扩展方法。
    • @BenMills 该模型似乎通过 IQuoteMailer 接口 QuoteMail(),通过控制器中的 Post 传递。
    • QuoteMailer.QuoteMail(FinishedQuote).Send(); -> cannot access internal method 'Send' here
    猜你喜欢
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 2015-06-09
    相关资源
    最近更新 更多