2010-07-01 63 views
0

我有這樣的功能:asp.net的MVC 2的呈現視圖字符串,而不是局部

public static string RenderViewToString(string controlName, object viewData) { 
    ViewDataDictionary vd = new ViewDataDictionary(viewData); 
    ViewPage vp = new ViewPage { ViewData = vd }; 
    Control control = vp.LoadControl(controlName); 

    vp.Controls.Add(control); 

    StringBuilder sb = new StringBuilder(); 
    using (StringWriter sw = new StringWriter(sb)) 
    { 
     using (HtmlTextWriter tw = new HtmlTextWriter(sw)) 
     { 
      vp.RenderControl(tw); 
     } 
    } 

    return sb.ToString(); 

}

我這樣稱呼它:

string body = StringHelpers.RenderViewToString("~/Areas/Public/Views/Shared/RegistrationEmail.ascx", new RegistrationEmailViewModel { User = user }); 

,並返回與用戶信息的HTML表。

但我想知道是否有辦法編輯這個我可以返回視圖爲字符串?所以我可以添加masterpage,所以設計所有潛在的郵件會更容易?

在此先感謝 /M

回答

相關問題