0

There is an input type="checkbox" in my razor, in my controller I saw that I'm getting multiple values for it. when I open the source code, there is an extra checkbox with that name that does not exist in my cshtml page:

this is the view:

<h1>Edit</h1>

<h4>Main Menu</h4>
<hr />
@if (domainId == 0)
{
    <p>Please select a domain to manage its Main Menu.</p>
}
else
{
    <div class="row">
        <div class="col-md-4">
            <form asp-action="Edit">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <input type="hidden" asp-for="Id" />
                <div class="form-group">
                    <label asp-for="Name" class="control-label"></label>
                    <input asp-for="Name" class="form-control" />
                </div>
                <div class="form-group">
                    <label asp-for="Enabled" class="control-label"></label>
                    <input asp-for="Enabled" style="vertical-align:middle;" />
                </div>
                <input type="hidden" name="domainid" value="@domainId" />
                <div class="form-group">
                    <input type="submit" value="Save" class="btn btn-primary" /> <a asp-action="Index" class="btn btn-dark">Back to List</a>
                </div>
            </form>
        </div>
    </div>
}

and this is the page source code:

<h1>Edit</h1>

<h4>Main Menu</h4>
<hr />
    <div class="row">
        <div class="col-md-4">
            <form action="/Navigations/Edit/2" method="post">

                <input type="hidden" data-val="true" data-val-required="The Id field is required." id="Id" name="Id" value="2" />
                <div class="form-group">
                    <label class="control-label" for="Name">Name</label>
                    <input class="form-control" type="text" id="Name" name="Name" value="Colors2" />
                </div>
                <div class="form-group">
                    <label class="control-label" for="Enabled">Enabled</label>
                    <input style="vertical-align:middle;" type="checkbox" data-val="true" data-val-required="The Enabled field is required." id="Enabled" name="Enabled" value="true" />
                </div>
                <input type="hidden" name="domainid" value="2" />
                <div class="form-group">
                    <input type="submit" value="Save" class="btn btn-primary" /> <a class="btn btn-dark" href="/Navigations">Back to List</a>
                </div>
            <input name="__RequestVerificationToken" type="hidden" value="CfDJ8BstbX-4WaJCtXJ2dnnNizPHecbQCN_dSuVU4omAVZmEOIjVXgNxzg0hlL5YKvgOUrSFYDvBHeKaws842QwGbAxfavMVf94GMN5nOJj9ZQ2qWcvyKNvEJj1qyr1_JIR1CyxeSjYe0UEcqBUpvkiVtpUdA-Yh_WXzxZbGvsCk4McM7o5HPYGLFX3bD15L58FtUg" /><input name="Enabled" type="hidden" value="false" /></form>
        </div>
    </div>


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

There is a hidden input named "Enabled" right before form closes, but where it comes from?

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • 2
    @KirkLarkin I'm not talking about that, the `` right after that. – Ashkan Mobayen Khiabani Oct 03 '19 at 11:53
  • Sorry, I didn't see that. Had to scroll a lot. :) – Kirk Larkin Oct 03 '19 at 11:54
  • Yeah, I didn't format the code because I don't want to make changes to the rendered html, I intended to keep it as original. – Ashkan Mobayen Khiabani Oct 03 '19 at 11:56
  • This [answer](https://stackoverflow.com/a/50514167) looks relevant. – Kirk Larkin Oct 03 '19 at 12:00
  • but where is that tag helper? it makes me mad, you know I can resolve my problem in many ways, like changing its name or removing extra input unsing javascript, ... but these are not right thing to do, I'm really interested to find out how it is possible. I tried cleaning solution, but no use. – Ashkan Mobayen Khiabani Oct 03 '19 at 12:00
  • I dunno how deep you want to go, but the [source](https://github.com/aspnet/AspNetCore/blob/release/2.2/src/Mvc/Mvc.TagHelpers/src/InputTagHelper.cs#L325) shows what does this. – Kirk Larkin Oct 03 '19 at 12:03
  • well this is exactly the case, butttt why should it create another extra input with that name and return multiple values like "true,false" – Ashkan Mobayen Khiabani Oct 03 '19 at 12:03
  • Also relevant: https://stackoverflow.com/questions/2697299/asp-net-mvc-why-is-html-checkbox-generating-an-additional-hidden-input. The reason is that an unchecked checkbox doesn't get posted with a value of `false` - it doesn't get posted at all. – Kirk Larkin Oct 03 '19 at 12:04
  • @KirkLarkin so if it works as in this [answer](https://stackoverflow.com/a/50514167) you showed in comment. I should only check if it has commas or not: `bool enabled = collection["enabled"].ToString().Contains(",");` although it works but its is very funny and not logical – Ashkan Mobayen Khiabani Oct 03 '19 at 12:07
  • @KirkLarkin thank you at least i now now that i'm not going crazy, Microsoft is doing a stupid thing. – Ashkan Mobayen Khiabani Oct 03 '19 at 12:11

0 Answers0