【问题标题】:Why I've got Exception of type 'System.StackOverflowException' was thrown. in my own htmlhelper? [closed]为什么我抛出了“System.StackOverflowException”类型的异常。在我自己的 htmlhelper 中? [关闭]
【发布时间】:2017-11-17 13:20:59
【问题描述】:

我已经为asp.net mvc中的复选框编写了htmlhelper,但是当我想使用它时。它向我显示了一个异常。System.StackOverflowException' was thrown如何解决它。实际上,我还如何将复选框值提交给htmlhelper,我想向我的 htmlhelper 提交值复选框。

 public static class HelperUI
    {
        public static MvcHtmlString CheckBoxSimple(this HtmlHelper htmlHelper, bool IsCheck, string name, object htmlAttributes)
        {
            string checkBoxWithHidden = htmlHelper.CheckBoxSimple(IsCheck, name, htmlAttributes).ToHtmlString();
            string pureCheckBox = checkBoxWithHidden.Substring(0, checkBoxWithHidden.IndexOf("<input", 1));
            return new MvcHtmlString(pureCheckBox);
        }
    }




<div class="col-md-6">
    <div class="form-group row">
        status
        <div class="col-md-9">
            @Html.CheckBoxSimple(true, "Status", new { @class = "form-control", placeholder = "status" })

        </div>
    </div>
</div>

【问题讨论】:

  • CheckBoxSimple 的第一行是...调用自己。你期望那行代码做什么?
  • @Damien_The_Unbeliever,我的目标是删除复选框中的隐藏值。why is Html.CheckBox generating an additional hidden input
  • 调试器会很快告诉你。
  • 那么,您是否打算写htmlHelper.CheckBox(... 而不是htmlHelper.CheckBoxSimple(...
  • 我喜欢早上递归的味道

标签: c# asp.net-mvc html-helper


【解决方案1】:

StackOverflowExceptions 往往是由无限循环或无限递归引起的。

在您的情况下,CheckBoxSimple 无限期地调用自己:第一行调用CheckBoxSimple 而不是CheckBox

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多