Razor output does not work in MVC 3, but works in MVC 2

The same code works fine with MVC 2, but does not work in MVC 3 Razor. After loading the page, the menu does not load from the HTMLHelper called in Razor, as shown below.

Hard menu for testing, which is not displayed on the page.

using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using myproject.Extensions;

public static class MenuHelper
{

    public static string TabbedMenu(this HtmlHelper helper, IEnumerable<MenuTab> tabs)
    {
       //I have hard coded menu for testing purpose.

        return "<div class='menu-image'><img src='/content/Images/common/on-left.gif' alt='' /></div><div class='on'><a class='over' href='/?Length=4'>Home</a></div><div class='menu-image'><img src='/content/Images/common/on-right.gif' alt='' /></div><a href='/Home/About'>About</a><a href='/Home/Contact'>Contact</a>";
    }

}

Below is the RHR CSHTML code.

 @{Html.TabbedMenu
                        (
                            new List<MenuTab>
                            {
                                MenuTab.Create("Home", "Index", "Home"),
                                MenuTab.Create("About", "About", "Home"),
                                MenuTab.Create("Contact", "Contact", "Home")
                            }
                        );}
+3
source share
1 answer

The packaging code in @{ ... }(like you) is equivalent to Razor <% ... %>(without =).

Therefore, your code calls the function, but does nothing with the result.

You must remove {}and ;simply write @Html.TabbedMenu(...); it is equivalent <%: Html.TabbedMenu(...) %>.

, HtmlString, Razor HTML.

+5

Source: https://habr.com/ru/post/1777114/


All Articles