具有MvcMailer的强类型模型

时间:2014-02-20 01:26:48

标签: asp.net asp.net-mvc-3 mvcmailer

我正面临将强类型模型传递给MvcMailer视图的麻烦。我正在使用asp.net mvc3并使用nuget安装了MvcMailer3。

没有错误消息并且消息已成功发送,但未填充数据字段。我正在做一切也尝试使用ViewBag字段,但同样的问题 - 消息发送成功但字段没有填充...

请帮助......我被困了两天!

这是我的控制器和视图代码....

//控制器代码

public ActionResult Index()
    {

        string invoicenumber = "BC-00000002";
        IEnumerable<Quantum.Models.usp_MasterPrintInvoice_Result> mpi = db.usp_MasterPrintInvoice(invoicenumber);

        IEnumerable<PrintDetailObject> printdetailobj = from anmpi in mpi select new PrintDetailObject { Head = anmpi.Head, ContributorName = anmpi.Name, RegistrationNumber = anmpi.RegistrationNumber, InvoiceNumber = anmpi.InvoiceNumber, InvoiceDate = anmpi.Date, Amount = anmpi.Amount, PaymentMonth = anmpi.Month, ReceivedBy = anmpi.Operator };


        ViewData.Model = printdetailobj.FirstOrDefault();



        IUserMailer mailer = new UserMailer();            
        mailer.Welcome().Send();
        return View();
    }

查看代码

 @model Quantum.Models.PrintDetailObject

@using Mvc.Mailer

<h2>Invoice</h2>


<fieldset>

<div class="print-details-page">
    <legend>@Html.DisplayFor(model => model.Head)</legend>

    <div class="display-label">InvoiceNumber</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.InvoiceNumber)
    </div>
    <div class="clear"></div>   
    <div class="display-label">Date</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.InvoiceDate)

    </div>
    <div class="clear"></div>   
    <div class="display-label">Recieved From:</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.ContributorName)
    </div>
    <div class="clear"></div>   
    <div class="display-label">Registration Number:</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.RegistrationNumber)
    </div>
    <div class="clear"></div>      

    <div class="display-label">Amount:</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Amount)
    </div>
    <div class="clear"></div>   
    <div class="display-label">Amount in Text:</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.AmountText)
    </div>
    <div class="clear"></div>   
    <div class="display-label">Month:</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.PaymentMonth)
    </div>
    <div class="clear"></div>   

    <div class="display-label">Recieved By:</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.ReceivedBy)
    </div>
    <div class="clear"></div>  
    <br />
    <p>Received with many thanks.</p>

</div>

</fieldset>

我只收到以下文字作为电子邮件正文:

Invoice


InvoiceNumber
Date
Recieved From:
Registration Number:
Amount:
Amount in Text:
Month:
Recieved By:

Received with many thanks.

1 个答案:

答案 0 :(得分:1)

嗨,我刚才有了回答我自己问题的见解。

实际上,当我在“SendEmail”控制器内部使用ViewData.Model时,模型可用于其自己的视图,即“索引”视图,而不是与“UserMailer”控制器一起使用的剃刀视图,该控制器名为“欢迎”我的情况。

要将Model传递给“UserMailer”操作,我们需要进行以下修改:

// IUserMailer代码

    ...
    public interface IUserMailer
            {
                MvcMailMessage Welcome(PrintDetailObject myModel);
            }

// UserMailer代码

    public virtual MvcMailMessage Welcome(PrintDetailObject myModel)
            {
                //ViewBag.Data = someObject;
                ViewData.Model = myModel;

                return Populate(x =>
                {
                    x.Subject = "Welcome";
                    x.ViewName = "Welcome";

                    x.To.Add("cmmwahid@hotmail.com");

                });
            }

//最后是控制器代码

public ActionResult Index()
        {

            string invoicenumber = "BC-00000002";
            IEnumerable<Quantum.Models.usp_MasterPrintInvoice_Result> mpi = db.usp_MasterPrintInvoice(invoicenumber);

            IEnumerable<PrintDetailObject> printdetailobj = from anmpi in mpi select new PrintDetailObject { Head = anmpi.Head, ContributorName = anmpi.Name, RegistrationNumber = anmpi.RegistrationNumber, InvoiceNumber = anmpi.InvoiceNumber, InvoiceDate = anmpi.Date, Amount = anmpi.Amount, PaymentMonth = anmpi.Month, ReceivedBy = anmpi.Operator };



            IUserMailer mailer = new UserMailer();
            mailer.Welcome(printdetailobj.FirstOrDefault()).Send();
            return View();
        }

这是电子邮件正文的最终输出:

Invoice

Generous Contribution
InvoiceNumber
BC-00000002
Date
15/02/2014
Recieved From:
Some Donor 
Registration Number:
M104/6
Amount:
$23.00
Amount in Text:
Twenty Three Dollars Only
Month:
May/2014
Recieved By:
someuser

Received with many thanks.