3

The issue is that for some bizarre reason, when I use CheckBoxFor I get a checkbox but the ID has "CS___8__locals1" in it. No idea where this comes from or why it's happening.

Any ideas? See below:

<input class="checkbox" id="ProductPlans_0__CS___8__locals1_plans_4__IsSelected" name="ProductPlans[0].CS$&lt;>8__locals1.plans[4].IsSelected" type="checkbox" value="true" />

My model is a single object (it is in reality part of a collection of objects but that should not matter. Here is my editor template (leaving out the @model declaration):

<tr> 
  <td>
    <label class="checkbox">
      @Html.CheckBoxFor(x => Model.IsSelected, new { @class = "checkbox" })
      <strong>@Model.MarketingLabel</strong>     
      @Html.Raw(@Model.DisplayName)
    </label>
  </td>
  <td>
    <span data-planid="@Model.Id">@Model.Premium.ToString("C")</span>
  </td>
  <td>
    <a href="@Model.SBCUrl">Explain</a>
  </td>
  @Html.HiddenFor(x => Model.Id)
</tr>
Barry
  • 101
  • 6
  • 1
    Is it a problem? It looks like an compiler-autogenerated name (similar to [this](http://stackoverflow.com/q/2508828/1364007)) to me. – Wai Ha Lee Jan 05 '16 at 18:45
  • Yes it's a problem - when I post the input back to my web server the bindings don't work. Server expects ProductPlans_0__plans_4__IsSelected for example. – Barry Jan 05 '16 at 20:01
  • can you post how is your model? and how are you building the CheckBoxFor ? – Daniel Gpe Reyes Jan 05 '16 at 20:10
  • The model binding uses the `name` attribute of your input field. The `id` attribute is completely irrelevant. It's never sent to the server and it never participates to any binding. – Darin Dimitrov Jan 05 '16 at 20:48
  • look this link https://github.com/aspnet/Mvc/issues/2890 – Daniel Gpe Reyes Jan 05 '16 at 23:03
  • Wow @DanielGpeReyes how did you find that??! Thanks! Doesn't look like there's a resolution but nice to know I'm not losing my mind. – Barry Jan 07 '16 at 15:35
  • @Barry just google "CS___8__locals1", maybe is the way how you're building the collection? – Daniel Gpe Reyes Jan 07 '16 at 15:53

1 Answers1

8

I know this is and old post but maybe will help someone else out there. So, in my case I had this same problem and the problem was solved after a few try and error and we discover that the error was in itself the for loop.

Our model was something like this:

for (int i = 0; i < PatiosCobro.Count; i++)
{
    <div class="col-md-4 col-sm-4 col-xs-12">
        <div class="input-group input-group">
            <div class="form-line">
                @Html.TextBoxFor(model => PatiosCobro[i].Valor, new { htmlAttributes = new { @class = "form-control" } })
            </div>
            @Html.HiddenFor(model => PatiosCobro[i].CobroId)
            @Html.NameFor(model => PatiosCobro[i].Valor) 
        </div>
    </div>
}

This will throw "CS___8__locals1_ETC"

What worked for us was taking out the local variable i and declaring it out in other place.

by example:

int i = 0;
for (i = 0; i < PatiosCobro.Count; i++)
{
    <div class="col-md-4 col-sm-4 col-xs-12">
        <div class="input-group input-group">
            <div class="form-line">
                @Html.TextBoxFor(model => PatiosCobro[i].Valor, new { htmlAttributes = new { @class = "form-control" } })
            </div>
            @Html.HiddenFor(model => PatiosCobro[i].CobroId)
            @Html.NameFor(model => PatiosCobro[i].Valor) 
        </div>
    </div>
}
Eduard Malakhov
  • 1,095
  • 4
  • 13
  • 25
William
  • 81
  • 1
  • 2
  • 1
    This is supposedly a bug fixed in MVC 6: https://github.com/aspnet/Mvc/issues/2890 I also get the issue just by assigning a child of my view model to a variable. `@var alias = Model.InnerVM; @Html.HiddenFor(m => alias.Prop)` generates a hidden named `CS$<>8__locals1.alias.Prop` – xr280xr Sep 29 '17 at 18:30