我不知道我是否正确解释了这个问题,或者解决方案是否相当简单,所以这里有:
我正在使用MvcMailer,但在此之前我设置了一个向导输入表单,我称之为Quote.cshtml。在Quote.cshtml后面,我建立了一个名为QuoteModel.cs的模型。
Quote.cshtml最基本的(我省略了所有的向导逻辑,只显示了一个输入):
<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(再次,仅显示一个输入; n.b:使用DataAnnotationExtensions)
public class QuoteModel
{
[Required(ErrorMessage = "First Name required.")]
[Display(Name = "First Name:")]
public string FirstName { get; set; }
}
现在我正在尝试集成MvcMailer,它设置了IQuoteMailer.cs,QuoteMailer.cs,_Layout.cshtml和QuoteMail.cshtml。 QuoteMail.cshtml是邮件的收件人最终会看到的内容。我还设置了一个QuoteController.cs,我在其中放置了MvcMailer所需的相应代码。它位于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: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);
}
}
所以,我的困惑是如何传递我创建的QuoteModel,这样我最终可以获取用户输入的数据,然后生成MvcMailer视图。
我感谢社区的帮助。
答案 0 :(得分: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>