I don't know if I am explaining this correctly, or if the solution is rather simple, so here goes:
I am using MvcMailer, but before that I set up a wizard input form which I call Quote.cshtml. Behind Quote.cshtml, I set up a model called QuoteModel.cs.
Quote.cshtml at its most basic (I am leaving out all of the wizard logic and only showing one input):
<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 (again, only showing the one input; n.b.: using the DataAnnotationExtensions)
public class QuoteModel
{
[Required(ErrorMessage = "First Name required.")]
[Display(Name = "First Name:")]
public string FirstName { get; set; }
}
Now I am trying to integrate MvcMailer, which sets up IQuoteMailer.cs, QuoteMailer.cs, _Layout.cshtml, and QuoteMail.cshtml. The QuoteMail.cshtml is what the recipient of the mail will eventually see. I also set up a QuoteController.cs, in which I placed the appropriate code required by MvcMailer. It is in the QuoteMailer.cs and QuoteController.cs where I am having trouble passing the user input from Quote.cshtml (which is based on the model in 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("[email protected]");
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 is pretty standard, so not showing here):
@*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>
And finally, the relevant parts of the QuoteController.cs (note that I have am using a wizard, therefore, part of my problem is figuring out where to put the MvcMailer code, but I think I may have it right):
public class QuoteController: Controller {
/// <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);
}
}
So, my confusion is to how to pass the QuoteModel I created, so that ultimately I can take the user inputed data and then generate the MvcMailer view.
I appreciate the communities help.
You could have the IQuoteMailer
interface take the model:
public interface IQuoteMailer
{
MailMessage QuoteMail(QuoteModel model);
}
and in the implementation use this 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("[email protected]");
// Use a strongly typed model
ViewData = new ViewDataDictionary(model);
PopulateBody(mailMessage, "QuoteMail", null);
return mailMessage;
}
}
then from the controller when you decide to send the mail pass the model:
[HttpPost]
public ActionResult Quote(QuoteModel FinishedQuote)
{
if (ModelState.IsValid)
{
QuoteMailer.QuoteMail(FinishedQuote).Send();
return View("QuoteMailSuccess", FinishedQuote);
}
else return View();
}
and finally in the template (~/Views/QuoteMailer/QuoteMail.cshtml
) you could use the model:
@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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With