简体   繁体   English

自定义HTML助手在asp.net MVC 2.0中不起作用

[英]Custom Html Helper is not working in asp.net MVC 2.0

I was using this custom html helper in asp.net mvc 1.0 but now I am trying to use it in a 2.0 project and it crashes 我在asp.net mvc 1.0中使用了此自定义html帮助程序,但是现在我试图在2.0项目中使用它,并且崩溃

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

This is the error I get. 这是我得到的错误。

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: 

My other html helpers in the same library do work. 我在同一个库中的其他HTML助手也可以工作。 I added the namespace into the webconfig. 我将名称空间添加到了webconfig中。

Code I have 我有的代码

 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;
        }

    }

Its complaining that it can't find a ValidationSummery method that returns a string... so I'm thinking it might be because HtmlHelper.ValidationSummary() now returns an instance of MvcHtmlString and not System.String . 它抱怨它找不到返回字符串的ValidationSummery方法...所以我想这可能是因为HtmlHelper.ValidationSummary()现在返回MvcHtmlString的实例,而不是System.String

I haven't tested this, but try changing your extension method to: 我尚未对此进行测试,但是尝试将扩展方法更改为:

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;
}

Let me know if that works or not :-) 让我知道是否可行:-)

HTHs, HTH,
Charles 查尔斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM