Error with string value "Boolean" using HtmlHelper DisplayFor

I ran into a problem when I try to show values ​​to the user, iterating the object and using HtmlHelpers. Currently, one column will carry the data type of the elements that I am writing to the screen, and they are string values. I ran into a problem when I try to display the value "Boolean", which is a data type string using the DisplayFor method. I get a FormatException: "String was not recognized as a valid boolean." I tried using it as a string in several ways, but no luck. If I change the line from "Boolean" to anything else, it works fine. Any suggestions?

<%foreach (var matrixColumnView in Model.MatrixColumns) {%> <tr id="<%="ColRow_" + matrixColumnView.Key %>" class="columnRow"> <td class="ui-helper-hidden"> <%=Html.HiddenFor(x => x.MatrixColumns[matrixColumnView.Key].EntityId)%> </td> <td> <%=Html.HiddenFor(x => x.MatrixColumns[matrixColumnView.Key].Sequence)%> <%=Html.DisplayFor(x => x.MatrixColumns[matrixColumnView.Key].Sequence, matrixColumnView.Value.Sequence.ToString())%> </td> <td> <%=Html.HiddenFor(x => x.MatrixColumns[matrixColumnView.Key].Name)%> <%=Html.DisplayFor(x => x.MatrixColumns[matrixColumnView.Key].Name, matrixColumnView.Value.Name)%> </td> <td> <%=Html.HiddenFor(x => x.MatrixColumns[matrixColumnView.Key].DataTypeName) %> <%=Html.DisplayFor(x => x.MatrixColumns[matrixColumnView.Key].DataTypeName, (string)matrixColumnView.Value.DataTypeName) %> </td> </tr> <%} %> 
+4
source share
1 answer

Instead:

 <%=Html.DisplayFor(x => x.MatrixColumns[matrixColumnView.Key].DataTypeName, (string)matrixColumnView.Value.DataTypeName) %> 

Just try:

 <%=Html.DisplayFor(x => x.MatrixColumns[matrixColumnView.Key].DataTypeName) %> 

Turning into (string)matrixColumnView.Value.DataTypeName - which is (presumably) evaluated as "Boolean" - as the second argument, you tell MVC to search for a display template named "Boolean". Obviously, the pattern found cannot display String values. Generally, you’d better let the platform decide which display template to use.

Here's a useful article that talks about how MVC resolves display / editor templates:

http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html

+2
source

Source: https://habr.com/ru/post/1434693/


All Articles