Simple helper class not working

Sorry to ask such a simple question, but I really tried for a very long time to solve this problem. In the end I will decide to ask you.

Start with the code base:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace Navigation.Helpers { public static class NavigationBarSE { public static MvcHtmlString RenderNavigationBarSE(this HtmlHelper helper, String[] includes) { return new MvcHtmlString("YU no Work??"); //NavTypeSE res = new NavTypeSE(includes); //String ress = res.toString(); //return new MvcHtmlString(ress); } } } 

In the original form, this helper should return the string created by the NavTypeSE class. But in the end, to get the result, I want it to return a String to me ... But that did not do this ...

Before I ask, I can say that

 <add namespace="Navigation.Helpers"/> 

exists in my web.config file in the Views folder.

For more details, my NavTypeSE class as shown below:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Navigation.Helpers { //Creates a Navigation Menu Type which includes Previous, Next and Validate Buttons public class NavTypeSE { Boolean pr, nt, vld; Boolean Previous { get; set; } Boolean Next { get; set; } Boolean Validate { get; set; } public NavTypeSE(Boolean Previous, Boolean Next, Boolean Validate) { this.pr = Previous; this.nt = Next; this.vld = Validate; } public NavTypeSE() { } public NavTypeSE(String[] inc) { for(int i=0; i<inc.Length; i++)//foreach (String s in inc) { String s = inc[i]; // Don't need for foreach method. if (s.Equals("previous")||s.Equals("Previous")) { this.pr = true; } else if (s.Equals("next") || s.Equals("Next")) { this.nt = true; } else if (s.Equals("validate") || s.Equals("Validate")) { this.vld = true; } else { this.pr = false; this.nt = false; this.vld = false; } } public String toString() { return "Previous: " + this.pr + ", Next: " + this.nt + ", Validate: " + this.vld; } } } 

In addition, in my view, I call this helper, as shown below:

 @{ String[] str = new String[] { "Previous", "next", "Validate" }; Html.RenderNavigationBarSE(str); } 

This is just the basis for the project. And I'm starting the level in both C # and the ASP.NET MVC platform. Sorry for wasting your time.

+4
source share
1 answer

Your RenderNavigationBarSE doesn't write anything back, just returns a MvcHtmlString .

Therefore, you need to put @ before the method call to tell the Razor engine that you want to write the returned MvcHtmlString to the response (otherwise, inside the code block, it simply executes your method and discards the return value)

 @{ String[] str = new String[] { "Previous", "next", "Validate" }; } @Html.RenderNavigationBarSE(str); 

You can learn more about Razor syntax:

+2
source

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


All Articles