我需要以html格式创建电子邮件正文。在HistoryController里面,我定义了一个返回一个对象(Course)的metod,正如我所说,这些属性将被用于装箱体。我不知道如何在视图中获取对象。我已经看过一个使用ActionResult的方法,但我不提供View它就像创建电子邮件的辅助工具一样。
我明白了。
HistoryController
public static CURSO courseSelected = new CURSO();
public static CURSO courseSel()
{
courseSelected.Name = "Physics";
return courseSelected;//cursoSelected del tipo CURSO
}
查看
@model Project.Models.Course
@{
var courseSel = Project.Models.HistoryController.courseSel();
}
<fieldset class="fHorizontalCenter">
<label style="font-size:18px">Welcome to course of </label><label>@courseSel.Name</label>< br />
</fieldset>
但是我不能使用变量的de数据,它总是打印这个
欢迎来到@ courseSel.Name
的课程答案 0 :(得分:0)
让我们假设您的模型看起来像这样:
namespace Project.Models
{
public class Course
{
public string CourseName { get; set; }
}
}
和一个名为&#34; CourseEmailTemplate.cshtml&#34;在您的视图/共享文件夹中:
@model Project.Models.Course
@{
Layout = null;
}
<fieldset class="fHorizontalCenter">
<label style="font-size:18px">Welcome to course of </label><label>@Model.CourseName</label>< br />
</fieldset>
然后控制器看起来像这样:
public class HistoryController : Controller
{
public ActionResult Index(/*put whatever parameters in here*/)
{
// Fetch your model
// just example data
var course = new Course { CourseName = "your coursename" };
var html = GetEmailText(course);
// send your email
return View(); // or redirect to action
}
private string GetEmailText(Course course)
{
return RenderRazorViewToString("CourseEmailTemplate", course);
}
private string RenderRazorViewToString(string viewName, object model)
{
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext,
viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View,
ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
}
中窃取的