I\'m having a problem where users on mobile devices are encountering an error in MVC that does not occur when viewing the site on a regular desktop. I can consistently repr
I had the exact same problem, and fixed it.
My problem turned out to be the use of a yield
block in my viewmodel:
Controller:
var vm = new BigVM {
SmallVMs = BuildSmallOnes()
};
return View(vm);
private IEnumerable<SmallVM> BuildSmallOnes()
{
// complex logic
yield return new SmallVM(1);
yield return new SmallVM(2);
}
View:
@model BigVM
@Html.DisplayFor(x => x.SmallVMs) <-- died
Inexplicably, this worked for desktops but failed for iPads and iPhones, citing the exact same stacktrace. Similar problems were reported here and here. The problem was solved by adding a .ToList()
call, thus:
var vm = new BigVM {
SmallVMs = BuildSmallOnes().ToList()
};
Presumably the class that the compiler generates to represent the yield block includes some characters that some User Agents just don't like. Including the ToList() call uses a List<> instead.
I believe that the reason we are having this issue is MultipleViews and DisplayMode providers introduced for handling mobile devices separately. This is code that is still in .NET 4 but has been deprecated.
A solution to disable the functionality entirely can be found here. Illegal characters in path depending on User-Agent?