I am having the following pice of code which is firing error
Error 1 Invalid expression term '='
@{
int Interest;
}
<td>@if (@item.interest.HasValue)
{
@Interest= @item.interest.Value.ToString("F2");
}
When declaring a variable, this variable needs to be assigned:
@{
string Interest = "";
}
and then:
@if (item.interest.HasValue)
{
Interest = item.interest.Value.ToString("F2");
}
This being said doing something like this in a view is a very bad design. I mean things like declaring and assigning variables based on some condition is not a logic that should be placed in a view. The view is there to display data. This logic should go to your controller or view model.
Inside your @if block you can address variables without the @
sign.
@if (@item.interest.value) {
@item= @item.interest.Value
}
Is interpreted as:
@if (@item.interest.value) {
Write(item=);
Write(@item.interest.Value);
}
As you can see Write(item=)
is not valid C# code.
You should use:
@if (item.interest.value) {
item = item.interest....
}
The reason your if (@item....)
statement compiles, with the @
sign. Is because you can prefix an identifier with the @ to use reserved words as identifier names.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With