很抱歉提出这么简单的问题,但我很长时间都试图解决这个问题.最后,我决定问你.
让我们从代码库开始:
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("Y U no Work??");
//NavTypeSE res = new NavTypeSE(includes);
//String ress = res.toString();
//return new MvcHtmlString(ress);
}
}
}
在原始形式中,此帮助程序需要返回由NavTypeSE类生成的String.但最后,为了得到一个结果,我只想让它为我返回一个字符串…但它没有那样做……
在你问之前,我可以这么说,
<add namespace="Navigation.Helpers"/>
存在于Views文件夹中的Web.config文件中.
有关详细信息,我的NavTypeSE类如下:
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;
}
}
}
此外,在我的视图中,我将此Helper称为如下所示:
@{
String[] str = new String[] { "Previous", "next", "Validate" };
Html.RenderNavigationBarSE(str);
}
这只是项目的基础.而且我在C#和ASP.NET MVC平台上都处于初级水平.很抱歉花时间.
最佳答案 你的RenderNavigationBarSE没有在Response中写入任何内容只返回一个MvcHtmlString.
因此,您需要在方法调用之前放置一个@来告诉Razor引擎您要将返回的MvcHtmlString写入响应(否则在代码块内部它只执行您的方法并抛弃返回的值)
@{
String[] str = new String[] { "Previous", "next", "Validate" };
}
@Html.RenderNavigationBarSE(str);
您可以阅读有关Razor语法的更多信息:
> Introduction to ASP.NET Web Programming Using the Razor Syntax (C#)
>还有一个C# Razor Syntax Quick Reference