0

I've written htmlhelper for checkbox in asp.net mvc but when I want to use it.It shows me a exception.System.StackOverflowException' was thrownHow to solve it.and I also how to submit checkbox value to htmlhelper in fact,I want to submit value checkbox to my 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>
behnam
  • 3
  • 1
  • 3
  • 5
    The first line of `CheckBoxSimple` is... calling itself. What did you *expect* that line of code to do? – Damien_The_Unbeliever Nov 17 '17 at 13:26
  • @Damien_The_Unbeliever,my goal is remove hidden value in checkbox.[why is Html.CheckBox generating an additional hidden input](https://stackoverflow.com/questions/2697299/asp-net-mvc-why-is-html-checkbox-generating-an-additional-hidden-input) – behnam Nov 17 '17 at 13:29
  • The debugger would have told you this pretty fast. – Mixxiphoid Nov 17 '17 at 13:29
  • 1
    So, did you perhaps intend to write `htmlHelper.CheckBox(...` rather than `htmlHelper.CheckBoxSimple(...`? – Damien_The_Unbeliever Nov 17 '17 at 13:32
  • Look at [this answer](https://stackoverflow.com/a/24341662/15498) to the question you linked to. It's *similar* to your code, except the first line of that method calls the `CheckBox` method. **your** version is calling `CheckBoxSimple`, which is *your* method - so as I said in my first comment, you're just calling your own method over and over again until the stack is used up. – Damien_The_Unbeliever Nov 17 '17 at 13:41
  • 1
    *i love the smell of recursion in the morning* – mcy Nov 17 '17 at 13:44
  • @Damien_The_Unbeliever,OMG,I didn't see it. – behnam Nov 17 '17 at 13:45
  • You need a base case which will exit from your recursive calls. – Shyju Nov 17 '17 at 15:10

1 Answers1

2

StackOverflowExceptions are often caused by an infinite loop or infinite recursion.

In your case CheckBoxSimple is calling itself indefinitely : the first line calls CheckBoxSimple instead of CheckBox.

AlexB
  • 7,302
  • 12
  • 56
  • 74