Quantcast
Channel: ASPNETWebStack Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 7215

Created Issue: MVC TemplateInfo.GetFullHtmlFieldName bug when in EditTemplate. [893]

$
0
0
I have an edit template that is given a list as its model. It then loops over this model and writes a bunch of checkboxes.

I spent a long time trying to figure out why, on postback, the values of the checkboxes weren't being written back to my model. After finally getting the debug symbols for MVC 4 working, I discovered that the DefaultBinder is expecting something in the postback like:
```

Model.EditTemplateMode[0].Value

```
However, the HtmlHelper CheckBoxFor is generating a checkbox with the the following name:
```

Model.EditTemplateModel.[0].Value
```

***Note the extra period before the left brace [0]. ***

I stepped through the CheckBoxFor code, and found that TemplateInfo.GetFullHtmlFieldName() is the problem. It concats the HtmlFieldPrefix with a "." and then with the input name.

*A* fix is that the TemplateInfoGetFullHtmlFieldName() should not add a "." between the two name parts if the second name part starts with a "[".

Here is some example Razor code from INSIDE the edit template:
```
@{
var itemIndex = 0;
foreach (var item in Model) {
Html.CheckBoxFor(model => model[itemIndex].Value)
itemIndex++;
}
}

```
As a note, I've tried every combination of expression to pass to Html.CheckBoxFor that I could think of, and the example above seems the closest. But just for thoroughness, here are other expressions I've tried:

```

model => item.Value
model => model[itemIndex].Value
model => model.Item[itemIndex].Value


```

Viewing all articles
Browse latest Browse all 7215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>