Conditional HTML Attributes using Razor MVC3
You didn't hear it from me, the PM for Razor, but in Razor 2 (Web Pages 2 and MVC 4) we'll have conditional attributes built into Razor(as of MVC 4 RC tested successfully), so you can just say things like this...
<input type="text" id="@strElementID" class="@strCSSClass" />
If strCSSClass is null then the class attribute won't render at all.
SSSHHH...don't tell. :)
ASP.NET MVC razor: conditional attribute in HTML
MVC has conditional attributes built in...
<div @{if (myClass != null) { <text>class="@myClass"</text> } }>Content</div>
<div class="@myClass">Content</div>
If @myClass is null, it just won't use the attribute at all...
I know that may not quite solve your current issue, but it is noteworthy!
http://weblogs.asp.net/jgalloway/archive/2012/02/16/asp-net-4-beta-released.aspx
ASP.NET MVC razor: conditional attribute in CSS block
You should put your css in css files and target specific classes instead of loading files conditionally.
Then you can use Razor to include conditional classes on your tags. Something like:
@{
var imgType = ViewBag.type == "mobile"?"for-mobile":"for-desktop";
}
<img class="@imgType" src="..."/>
Then in your CSS file:
.for-mobile{
min-height:100vh;
height:auto;
}
.for-desktop{
height: 100vh;
}
ASP.NET MVC 3 using Razor - use conditional expression together with the HTML output
@(item.Manager == null ? new HtmlString("<i>unassigned</i>") : new HtmlString( item.Manager.Name) )
How to use ternary operator in razor (specifically on HTML attributes)?
You should be able to use the @()
expression syntax:
<a class="@(User.Identity.IsAuthenticated ? "auth" : "anon")">My link here</a>
Writing conditional HTML with razor
Here's a shorter approach:
@foreach (var item in Model)
{
<tr @(item.DataState != DataState.Active ? "style=background-color:red" : "")>
<td>@item.Name</td>
</tr>
}
Edit: Code fixed
razor conditional attributes to output html
<div @if(!isEditOrCopyMode && !Model.IsRangeDefaultsSet){<text>id="hidden-frame-thickness" style="display: none"</text>} >
aspnet razor editorfor conditionally add attribute
You need to build an object defining the html attributes based on some property and then use that in the DropDownList()
method. Assuming you model contains a property bool IsReadOnly
, then
@{
var attributes = Model.IsReadOnly ?
(object)new { @class = "form-control", readonly = "readonly" } :
(object)new { @class = "form-control"};
}
@Html.DropDownList("FK_CompID", null, attributes)
Related Topics
Is Cross-Origin Postmessage Broken in Ie10
Play Sound File in a Web-Page in the Background
How to Change the Opacity (Alpha, Transparency) of an Element in a Canvas Element
Add Hyperlink to Textblock Wpf
How to Show Multiline Text in a Table Cell
White Space Inside Xml/HTML Tags
What's the Purpose of the HTML "Nonce" Attribute for Script and Style Elements
Uppercase or Lowercase Doctype
Wait Until an HTML5 Video Loads
How to Vertically Center a <Span> Inside a Div
Make Bootstrap's Carousel Both Center and Responsive
Input Type=Password, Don't Let Browser Remember the Password