Test variable content

2

Using ASP.Net MVC and AngularJS I tested the contents of a view field like this:

$scope.estado.ldRedeBasica =  @(Model.ldRedeBasica == null ? "[]" : Html.Raw(Model.ldRedeBasica));

Only returned the following error:

  

Compiler Error Message: CS0173: The conditional expression type can not be determined because there is no implicit conversion   between 'string' and 'System.Web.IHtmlString'

The interesting thing is that in another part of the code it works:

$scope.ViewBag.Impostos = @(ViewBag.Impostos == null ? "[]" : Html.Raw(ViewBag.Impostos));

How to work around this problem?

    
asked by anonymous 27.03.2015 / 18:29

1 answer

1

Do this:

$scope.estado.ldRedeBasica =  @(Model.ldRedeBasica == null
                                ? new MvcHtmlString("[]") // ou poderia ser 'Html.Raw("[]")'
                                : Html.Raw(Model.ldRedeBasica));

Documentation MvcHtmlString

The error occurs because Html.Raw does not return string , but rather an implementation of type IHtmlString .

Imagine the situation:

var x = (condicao == true) ? "string" : 32784;

What would be the type of x ? ... impossible to say. The error you have is of the same type as the above error.

In order to be able to determine the type of a condition using ternary operator, one of the expressions must be convertible to the type of the other expression ... which is not the case.

  • nor from string to IHtmlString
  • nor from IHtmlString to string

Now, to know the reason for the other code to work, there will be a need for a deeper investigation ... not being able to say with just what was put.

    
27.03.2015 / 18:33