8

これを正しく説明しているかどうか、または解決策がかなり単純かどうかはわかりません。したがって、次のようになります。

私は MvcMailer を使用していますが、その前に Quote.cshtml というウィザード入力フォームをセットアップしました。Quote.cshtml の背後に、QuoteModel.cs というモデルをセットアップしました。

最も基本的な Quote.cshtml (ウィザードのロジックはすべて除外し、1 つの入力のみを表示しています):

<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 (ここでも、1 つの入力のみを表示します。nb: DataAnnotationExtensionsを使用)

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

現在、IQuoteMailer.cs、QuoteMailer.cs、_Layout.cshtml、および QuoteMail.cshtml をセットアップする MvcMailer を統合しようとしています。QuoteMail.cshtml は、最終的にメールの受信者に表示されるものです。また、MvcMailer に必要な適切なコードを配置した QuoteController.cs もセットアップしました。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 コードを配置する場所を見つけることが問題の一部ですが、私はそれが正しいと思います):

public class 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 ビューを生成できます。

コミュニティの助けに感謝します。

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 に答える