1

i am facing troubles passing strongly typed model to MvcMailer view. I am using asp.net mvc3 and installed MvcMailer3 using nuget.

There is no error messages and message is sent successfully but the data fields are not populated. I am doing everything also tried using ViewBag fields but the same problem- that is message sent successfully but fields are not populated...

please help...i am stuck for two days!

here is my controller and view code....

//Controller code

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();
    }

View Code

 @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>

I am only get the following text as the email body:

Invoice


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

Received with many thanks.
Choudhury
  • 273
  • 1
  • 5
  • 14

1 Answers1

1

hi i have just got the insight to answer my own question.

actually when i used ViewData.Model inside of the "SendEmail" Controller the model is available to its own view namely the "Index" view instead of the razor view used with the "UserMailer" Controller which is named "Welcome" in my case.

To pass the Model to "UserMailer" action we need to do the following modification:

//IUserMailer code

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

//UserMailer code

    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");

                });
            }

//and finally controller code

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();
        }

here is the final output as a email body:

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.
Choudhury
  • 273
  • 1
  • 5
  • 14