Tuesday, August 16, 2011

ASP.NET MVC 3 Section Helper Sitemap same Helper Codes

 
Section Html
 
@section Types {
    @{
        var menuTypes = Util.enumToList(typeof(MenuTypeEnum));
        <text><span>Цэсний төрөл: </span><select onchange="listMenu();" id="MenuTypeCode" class="select">
            @foreach(var mt in menuTypes) {
                <option value="@mt.Value">@mt.Text</option>
            }
        </select>
        </text>   
    }
}
 
Rozor Helper.cshtml in App_Code
 
@using System;
@using System.Collections.Generic;
@using System.Linq;
@using System.Web;
@using System.Web.Mvc;
@using System.Web.Mvc.Html;
@using System.Web.WebPages;

@functions {
   
    public MvcHtmlString RenderHtml()
    {
        return new MvcHtmlString("html");
    }
}

@helper SiteMap() {
       
    }
 
SiteMap Html Render
 
@{
   
    var topLevelNodes = SiteMap.RootNode.ChildNodes;
    <text><ul class="menu">
        @foreach (SiteMapNode node in topLevelNodes)
        {
            if (SiteMap.CurrentNode == node) {  
                <li class="selectedMenuItem"><a href="@node.Url">@node.Title</a></li>
            } else {
                <li><a href="@node.Url">@node.Title</a></li>
            }
        }
    </ul>
    </text>
}
 
 
Add Page Namespace
 
<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
            <add namespace="CompanyName.ProjectName.Resources"/>
        </namespaces>
    </pages>
</system.web.webPages.razor>


Resource

 
public static class CommonHtmlExtensions
{
    public static object GetGlobalResource(this HtmlHelper htmlHelper, string classKey, string resourceKey)
    {
        return htmlHelper.ViewContext.HttpContext.GetGlobalResourceObject(classKey, resourceKey);
    }

    public static object GetGlobalResource(this HtmlHelper htmlHelper, string classKey, string resourceKey, CultureInfo culture)
    {
        return htmlHelper.ViewContext.HttpContext.GetGlobalResourceObject(classKey, resourceKey, culture);
    }

    public static object GetLocalResource(this HtmlHelper htmlHelper, string classKey, string resourceKey)
    {
        return htmlHelper.ViewContext.HttpContext.GetLocalResourceObject(classKey, resourceKey);
    }

    public static object GetLocalResource(this HtmlHelper htmlHelper, string classKey, string resourceKey, CultureInfo culture)
    {
        return htmlHelper.ViewContext.HttpContext.GetLocalResourceObject(classKey, resourceKey, culture);
    }

}

No comments: