堆栈溢出异常的MVcHtmlString [英] Stack Overflow Exception in MVcHtmlString

查看:165
本文介绍了堆栈溢出异常的MVcHtmlString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了自己的HTML帮助它增加了红色星号的必填字段。

I have created my own Html Helper which adds red asterisks to any required field.

它成功的作品既

@Html.myLabelFor(model => model.Description)
//and
@Html.myLabelFor(model => model.Description, new { /*stuff*/ })

然而,一些code线都像以下

However, some of the code lines are like following

@Html.myLabelFor(model => model.Description, "Deletion Reason", new { /*stuff*/ })

我的方法不是设计来处理3个参数,所以我增加呼叫者这将处理3个参数

My method was not designed to handle 3 parameters, so I added a caller which would handle 3 parameters

public static MvcHtmlString myLabelFor<TModel, TValue>(this HtmlHelper<TModel> html,
     Expression<Func<TModel, TValue>> expression, string labelText, Object htmlAttributes)
  {
      return myLabelFor(html, expression, labelText, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
  }    

下面是其他方法都正常工作(包括内部,它包含所有必要code和其结构我作为参考)

Below are other methods that are working properly (including internal, which contains all necessary code and whose structure I used as a reference)

public static MvcHtmlString myLabelFor<TModel, TValue>(this HtmlHelper<TModel> html,
   Expression<Func<TModel, TValue>> expression, IDictionary<String, Object> htmlAttributes)
  {
      return LabelHelper(html, ModelMetadata.FromLambdaExpression(expression, html.ViewData),
          ExpressionHelper.GetExpressionText(expression), null, htmlAttributes);
  }

  public static MvcHtmlString myLabelFor<TModel, TValue>(this HtmlHelper<TModel> html,
  Expression<Func<TModel, TValue>> expression)
  {
      return LabelHelper(html, ModelMetadata.FromLambdaExpression(expression, html.ViewData),
          ExpressionHelper.GetExpressionText(expression), null);
  }

  public static MvcHtmlString myLabelFor<TModel, TValue>(this HtmlHelper<TModel> html,
      Expression<Func<TModel, TValue>> expression, Object htmlAttributes)
  {
      return myLabelFor(html, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
  }

  //USED ITS STRUCTURE AS A REFERENCE
  internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName,
      String labelText = null, IDictionary<String, Object> htmlAttributes = null)

从逻辑上讲,我期待这个参数LabelText的将采取从上面code线删除理​​性的值。然而,而是抛出了一个StackOverflowException我的3个参数的方法内。 微软描述很模糊,<一个href=\"http://programmers.stackexchange.com/questions/189534/how-many-are-too-many-nested-function-calls\">additional解释没有帮助,而<一href=\"http://stackoverflow.com/questions/27046267/mvchtmlstring-with-razor-statements-included\">additional解决方案用

Logically, I was expecting that parameter labelText would take a value of "Deletion Reason" from the line of code above. However, instead it had thrown a StackOverflowException inside my 3-parameter method. Microsoft description was vague, additional explanation did not help, and additional solution was using

Expression<Func<TModel, string>> expression instead of my Expression<Func<TModel, TValue>> expression

我不明白我在做什么错。在这一点上,我只能想到捣鼓参数,直到它的作品,但我希望有更优雅的解决这一问题。

I do not understand what I am doing wrong. At this point I can only think of "fiddle with parameters until it works", but I am hopeful there is more elegant solution to that problem.

PS:请让我知道,如果我的code内部帮手,将有助于解决这个问题。

PS: Please let me know if my code for internal helper will help to solve the problem.

推荐答案

您获得的第一个重载个例外,因为该方法递归调用自身,一直这样做,直到执行堆栈溢出。而不是自称您需要更改

You getting an exception on the first overload, because the method is recursively calling itself, and keeps doing so until the execution stack overflows. Rather than calling itself you need to change

return myLabelFor(html, 
                  expression,
                  labelText,
                  HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));

return LabelHelper(html,
                   ModelMetadata.FromLambdaExpression(expression, html.ViewData),
                   ExpressionHelper.GetExpressionText(expression),
                   labelText,
                   HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));

从你的意见,你的第四超载的原因,它使用返回myLabelFor(...)不抛出异常是因为它要求你的第二个过载而这又来电返回LabelHelper(...)

From your comments, the reason your 4th overload which uses return myLabelFor(...) does not throw the exception is because it calls your 2nd overload which in turn calls return LabelHelper(...)

我建议您更改4载调用 LabelHelper()直接,并改变所有的公共重载显式调用 LabelHelper(),传递所有4个参数,它是由内置`的HtmlHelper扩展方法中使用的模式(可以查看<一个href=\"https://aspnetwebstack.$c$cplex.com/SourceControl/latest#src/System.Web.Mvc/Html/LabelExtensions.cs\"相对=nofollow>来源$ C ​​$ C为LabelFor()这里)

I recommend that you change the 4th overload to call LabelHelper() directly, and change all the public overloads to explicitly call LabelHelper(), passing all 4 parameters, which is the pattern used by the in-built `HtmlHelper extension methods (you can view the source code for LabelFor() here)

这篇关于堆栈溢出异常的MVcHtmlString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆