2014-02-06 91 views
0

我已經開發使用mvc3剃鬚刀的web應用程序,但我的客戶想讓我使用mvc2。 我的問題是我開發的可以傳遞HTML代碼到C#代碼並將其附加到我的函數的幫助程序。將MVC3剃鬚刀轉換爲MVC2

這是我的示例幫手。

public class SampleHelper 
{ 
    public string Content { get; set; } 
    public SampleHelper Content(Func<object, HelperResult> template) 
    { 
     string html = template.Invoke(null).ToHtmlString(); 
     this.Content = html; 
     return this; 
    } 
    public MvcHtmlString Render() 
    { 
     string html = @"<div class='content'>" + this.Content + "</div>"; 
     return MvcHtmlString.Create(html); 
    } 
} 

這是我用它查看HTML的方式

@SampleHelper.Create().Content(@<text> 
    <input id='sample' type='button' value='test button inside div' /> 
</text>).Render() 

誰能幫我將其轉換爲MVC2。 我試過這個鏈接,但沒有運氣。 Translate to razor Syntax from MVC 2.0 code

回答

1

這個thread給出了一個如何實現這種包裝擴展的例子。在你的情況你可能想這樣的事情作爲DisposableHelper實現:

public static class DisposableExtensions 
{ 
    public static IDisposable SampleHelper(this HtmlHelper htmlHelper) 
    { 
     string startTag = @"<div class='content'>"; 
     string endTag = "</div>"; 
     return new DisposableHelper(
      () => htmlHelper.ViewContext.HttpContext.Response.Write(startTag), 
      () => htmlHelper.ViewContext.HttpContext.Response.Write(endTag) 
     ); 
    } 
} 

然後你可以使用它,就像BeginForm方法:

@using (Html.SampleHelper()){ 
    <input id='sample' type='button' value='test button inside div' /> 
} 
+0

謝謝。我讓你知道這裏。 – Maynard

+0

@Maynard,不客氣。讓我知道是否需要更多信息 – Andrei