Assigning value to razor difficulty

I have the following code code that causes an error

Error 1 Invalid expression term '='

@{

  int Interest;

}

 <td>@if (@item.interest.HasValue)
    {

        @Interest= @item.interest.Value.ToString("F2");
    }
+3
source share
3 answers

When declaring a variable, this variable must be assigned:

@{
    string Interest = "";
}

and then:

@if (item.interest.HasValue)
{
    Interest = item.interest.Value.ToString("F2");
}

They say that doing something like this in a submission is a very poor design. I mean things like declaring and assigning variables based on some condition, not the logic that needs to be placed in the view. View to display data. This logic should go to your controller or view model.

+4
source

Inside the @if block, you can access unsigned variables @.

@if (@item.interest.value) {
   @item= @item.interest.Value
}

interpreted as:

@if (@item.interest.value) {
     Write(item=);
     Write(@item.interest.Value);
}

, Write(item=) #.

:

 @if (item.interest.value) {
     item = item.interest....
 }

, if (@item....) @. , @ .

+1

Try the following:

@{

string Interest;
}

 <td>@if (@item.interest.HasValue)
    {

        Interest= @item.interest.Value.ToString("F2");
    }

By the way, you are trying to assign a string (the result of ToString ()) to an integer. This will not work.

0
source

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


All Articles