Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor in-line if statement not working?

I've got an exception throwing at this line, and can't figure out why...maybe someone else can spot it

<img src="@{Model.Image != null ? Model.Image.FileName : "";}" width="200px" id="ImagePreview"/>

The exception I'm getting is:

error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
like image 403
Grahame A Avatar asked Jun 01 '11 19:06

Grahame A


People also ask

How do you write if condition in razor view?

The If Condition The if statement returns true or false, based on your test: The if statement starts a code block. The condition is written inside parenthesis. The code inside the braces is executed if the test is true.

What is razor in MVC What are the main Razor syntax rules?

Razor is a simple programming syntax for embedding server code in web pages. Razor syntax is based on the ASP.NET framework, the part of the Microsoft.NET Framework that's specifically designed for creating web applications.


2 Answers

You need to use the expression (explicit) code block style for that expression:

<img src="@(Model.Image != null ? Model.Image.FileName : "")" width="200px" id="ImagePreview"/> 

see the gu's post

like image 121
Brett Avatar answered Nov 03 '22 04:11

Brett


Try wrapping it in parenthesis instead of curly brackets:

<img src="@(Model.Image != null ? Model.Image.FileName : "")" width="200px" id="ImagePreview"/>
like image 38
Jerad Rose Avatar answered Nov 03 '22 05:11

Jerad Rose