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>
7
  • 1
    Is it a problem? It looks like an compiler-autogenerated name (similar to this) to me. Commented Jan 5, 2016 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. Commented Jan 5, 2016 at 20:01
  • can you post how is your model? and how are you building the CheckBoxFor ? Commented Jan 5, 2016 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. Commented Jan 5, 2016 at 20:48
  • look this link github.com/aspnet/Mvc/issues/2890 Commented Jan 5, 2016 at 23:03

1 Answer 1

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>
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is supposedly a bug fixed in MVC 6: 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.