2010-05-07 81 views
0

我是用在asp.net mvc的1.0这个自定义的HTML帮助,但现在我想在2.0项目中使用它,它崩溃自定义HTML助手是不是在asp.net MVC工作2.0

http://blog.pagedesigners.co.nz/archive/2009/07/15/asp.net-mvc-ndash-validation-summary-with-2-forms-amp-1.aspx

这是我得到的错误。

System.MissingMethodException was unhandled by user code 
    Message=Method not found: 'System.String System.Web.Mvc.Html.ValidationExtensions.ValidationSummary(System.Web.Mvc.HtmlHelper)'. 
    Source=CustomHtmlHelpers 
    StackTrace: 
     at CustomHtmlHelpers.ActionValidationSummaryHelper.ActionValidationSummary(HtmlHelper html, String action) 
     at ASP.views_signin_signin_aspx.__RenderContent2(HtmlTextWriter __w, Control parameterContainer) in SignIn.aspx:line 23 
     at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) 
     at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) 
     at System.Web.UI.Control.Render(HtmlTextWriter writer) 
     at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) 
     at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) 
     at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) 
     at ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) Site.Master:line 64 
     at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) 
     at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) 
     at System.Web.UI.Control.Render(HtmlTextWriter writer) 
     at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) 
     at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) 
     at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) 
     at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) 
     at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) 
     at System.Web.UI.Page.Render(HtmlTextWriter writer) 
     at System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) 
     at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) 
     at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) 
     at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) 
     at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 
    InnerException: 

我在同一个库中的其他html helper可以工作。我将该命名空间添加到webconfig中。

代码,我

public static class ActionValidationSummaryHelper 
    { 
     public static MvcHtmlString ActionValidationSummary(this HtmlHelper html, string action) 
     { 
      string currentAction = html.ViewContext.RouteData.Values["action"].ToString(); 

      if (currentAction.ToLower() == action.ToLower()) 
      { 
       return html.ValidationSummary(); 
      } 

      return MvcHtmlString.Empty; 
     } 

    } 
+0

发帖该方法的源代码可能会有所帮助。 – 2010-05-15 14:30:29

+0

那么它的代码与链接相同,现在就是来自Charlino的新建议。 – chobo2 2010-05-15 18:03:51

回答

1

它抱怨它不能找到一个ValidationSummery方法返回一个字符串...所以我想这可能是因为现在HtmlHelper.ValidationSummary()返回MvcHtmlString实例而不是System.String

我没有测试过这一点,但尝试改变你的扩展方法:

public static MvcHtmlString ActionValidationSummary(this HtmlHelper html, string action) 
{ 
    string currentAction = html.ViewContext.RouteData.Values["action"].ToString(); 

    if (currentAction.ToLower() == action.ToLower()) 
     return html.ValidationSummary(); 

    return MvcHtmlString.Empty; 
} 

让我知道是否可行与否:-)

HTHS,
查尔斯

+0

嗯不,它似乎仍然无法正常工作。我收到了同样的错误。 – chobo2 2010-05-10 06:07:30

+0

它仍然给出完全相同的例外? Web项目中的扩展方法还是单独的类库? – Charlino 2010-05-10 06:14:05

+0

似乎是同样的例外。它在一个单独的类库中。 – chobo2 2010-05-10 17:23:10