簡體   English   中英

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

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

我在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

這是我得到的錯誤。

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助手也可以工作。 我將名稱空間添加到了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;
        }

    }

它抱怨它找不到返回字符串的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;
}

讓我知道是否可行:-)

HTH,
查爾斯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM