Some .NET snippets that I need to remember

Posted on June 18, 2013 in Web Dev

So I’m developing some front-end stuff in using .NET. Coming from a PHP, I find some of the conventions and features of .NET to be quite confusing. I’m going to update this post with some snippets I’ll likely need to use in the future:

How to easily evaluate data from controller in a view

Controller code example

ViewData[“SectionParent”] = “Affiliate”;

View code example

@if (ViewData["SectionParent"] == "Affiliate") { // code here // }


How to output plain text in a view @if statement

This bit really confused me. I was trying to evaluate ViewData, where if it’s true, output some divs. It worked absolutely fine:

@if (ViewData["SectionParent"] == "Affiliate") { <div>Bla bla content and more divs</div> }

However, doing the exact same thing with only text (no HTML markup) kept throwing an error:

@if (ViewData["SectionParent"] == "Affiliate") { Some text here }

The solution is to wrap the plain text in <text></text> tags, and voila, it works just fine :-):

@if (ViewData["SectionParent"] == "Affiliate") { <text>Some text here</text> }


More coming soon!

Leave a comment

Was this helpful? Did I miss something? Do you have a question? Get in touch, or tell me below.