smsohan / MvcMailer

A Mailer for ASP.Net MVC that forms the Email Body using MVC Views (Razor etc.) following Ruby on Rails ActionMailer style
MIT License
582 stars 178 forks source link

Pass Record ID to MvcMailer #16

Closed Dotbyte closed 13 years ago

Dotbyte commented 13 years ago

Great framework and easy for a beginner like myself to implement.

However I have problems when I am trying to pass a record id to the mail view.

I am wanting to use MvcMailer to email a view of a new record (Code below). Can you example this scenerio? I can understand if this request is outside the remit of this forum.

[HttpPost] public ActionResult Index(Form Form) { if (ModelState.IsValid) { storeDB.Forms.Add(Form); storeDB.SaveChanges();

// get id
int newID = Form.ID;

var forms = from r in storeDB.Forms
            orderby r.FORMNAMEID, r.DateAction ascending
            where r.ID == newID
            select r

UserMailer.Welcome().Send(); 
//return RedirectToAction("Index");
return View("Successful");

}

Kind regards

Andrew

smsohan commented 13 years ago

Add a parameter to your welcome method and then pass it as view bag as you would do from your controller.


Sent from my iPhone

S M Sohan Consultant ThoughtWorks Canada Suite 1100, 600 6th Ave SW Calgary, AB T2P 0S5 Cell: +1 403 714 2673

On 2011-06-09, at 8:23 AM, Dotbyte reply@reply.github.com wrote:

Great framework and easy for a beginner like myself to implement.

However I have problems when I am trying to pass a record id to the mail view.

I am wanting to use MvcMailer to email a view of a new record (Code below). Can you example this scenerio? I can understand if this request is outside the remit of this forum.

[HttpPost] public ActionResult Index(Form Form) { if (ModelState.IsValid) { storeDB.Forms.Add(Form); storeDB.SaveChanges();

// get id int newID = Form.ID;

var forms = from r in storeDB.Forms orderby r.FORMNAMEID, r.DateAction ascending where r.ID == newID select r

UserMailer.Welcome().Send(); //return RedirectToAction("Index"); return View("Successful");

}

Kind regards

Andrew

Reply to this email directly or view it on GitHub: https://github.com/smsohan/MvcMailer/issues/16

Dotbyte commented 13 years ago

Thanks for your quick response. I have tried adding parameters to

public virtual MailMessage Welcome(int id)

but receive

'FormMVC.Mailers.UserMailer' does not implement interface member 'FormMVC.Mailers.IUserMailer.Welcome()'

I think my beginner's hat is large!

Regards

Andrew

smsohan commented 13 years ago

Change the interface inside mailers.


Sent from my iPhone

S M Sohan Consultant ThoughtWorks Canada Suite 1100, 600 6th Ave SW Calgary, AB T2P 0S5 Cell: +1 403 714 2673

On 2011-06-09, at 9:02 AM, Dotbyte reply@reply.github.com wrote:

Thanks for your quick response. I have tried adding parameters to

public virtual MailMessage Welcome(int id)

but receive

'FormMVC.Mailers.UserMailer' does not implement interface member 'SegroMVC.Mailers.IUserMailer.Welcome()'

I think my beginner's hat is large!

Regards

Andrew

Reply to this email directly or view it on GitHub: https://github.com/smsohan/MvcMailer/issues/16#comment_1334773

Dotbyte commented 13 years ago

Thanks you for your help. Finally got my head round it :)

IUserMailer.cs:

public interface IUserMailer { MailMessage Welcome(int id);
MailMessage PasswordReset(); }

UserMailer.cs:

public class UserMailer : MailerBase, IUserMailer
{

    // db connection
    FormEntities storeDB = new FormEntities();

    public UserMailer():
        base()
    {
        MasterName="_Layout";
    }

    public virtual MailMessage Welcome(int id)
    {

        Form form = storeDB.Forms.Find(id);
        var mailMessage = new MailMessage{Subject = "Thank you for your Gifts and Hospitalty submission on " + DateTime.Now };

        ViewBag.FirstName = form.Firstname;
        ViewBag.Surname = form.Surname;
        ViewBag.EmailAddress = form.EmailAddress;

        string emailTo = form.EmailAddress;

        mailMessage.To.Add(emailTo);

        PopulateBody(mailMessage, viewName: "Welcome");

        return mailMessage;
    }

    public virtual MailMessage PasswordReset()
    {
        var mailMessage = new MailMessage{Subject = "PasswordReset"};

        //mailMessage.To.Add("some-email@example.com");

        PopulateBody(mailMessage, viewName: "PasswordReset");

        return mailMessage;
    }

}