Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor syntax within html attributes

Check out the following:

<a href="/test?x=@if (Model.IsTest) { @(1) } else { @(4) }"></a>

Is there a better way to write this instead of the @(1) and @(4)?

like image 736
timeshift Avatar asked Jan 23 '11 07:01

timeshift


People also ask

What is the Razor syntax?

Razor syntax is a simple programming syntax for embedding server-based code in a web page. In a web page that uses the Razor syntax, there are two kinds of content: client content and server code.

How do you give ID in Razor syntax?

Just add the id property to the html-attributes. That will override the default id generated by the editorfor-helper-methode. Save this answer.

What is razor view HTML?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language.

What are the main Razor syntax rules Mcq?

Following are the rules for main Razor Syntax: Razor code blocks are enclosed in @{ … } Inline expressions (variables and functions) start with @ Code statements end with a semicolon.


1 Answers

You can use the conditional operator:

<a href="/test?x=@(Model.IsTest ? 1 : 4)"></a>
like image 91
Guffa Avatar answered Oct 11 '22 00:10

Guffa