开发者

An unhandled exception of type 'System.StackOverflowException with a custom helper class

I have a html helper thanks to Darin however I have done something to cause it to stop working

 public static MvcHtmlString ValidationStyledMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ex)
    {

            var result = htmlHelper.ValidationStyledMessageFor(ex);
            var res = string.Format("<span class=\"error required\"><p>{0}<a class=\"close\" href=\"javascript:closeError();\"></a></p></span>", result.ToHtmlString());
            return MvcHtmlString.Create(res);

    }

I am calling the helper class like this

          @Html.ValidationStyledMessageFor(model => model.UserName)

However when it runs I am getting an error

An unhandled exception of type 'System.StackOverflowException' occurred in MyMVC.DLL

The error is returning

Cannot evaluate expression because the current thread is in a stack overflow state.

Which means nothing to me, Is there some kind of way to debug this so I can work out what is h开发者_开发技巧appening?


Here:

var result = htmlHelper.ValidationStyledMessageFor(ex);

you are calling your custom helper once again which calls:

var result = htmlHelper.ValidationStyledMessageFor(ex);

which is calling your custom helper once again which calls:

var result = htmlHelper.ValidationStyledMessageFor(ex);

... and so on until you run out of stack and the exception is thrown.

So you probably want to call the default helper instead of calling yourself:

public static MvcHtmlString ValidationStyledMessageFor<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper, 
    Expression<Func<TModel, TProperty>> ex
)
{
    var result = htmlHelper.ValidationMessageFor(ex);
    var res = string.Format("<span class=\"error required\"><p>{0}<a class=\"close\" href=\"javascript:closeError();\"></a></p></span>", result.ToHtmlString());
    return MvcHtmlString.Create(res);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜