Writing HTML code inside variable in ASP.NET C# and Razor

The Razor engine HTML encodes strings by default, as you have noticed. To avoid this behavior, just use Html.Raw(): <ul>@Html.Raw(List)</ul> Edit To render a variable within a string, I suppose you could use string.Format: @{ var someVariable = “world”; } @string.Format(“<div>hello {0}</div>”, someVariable) Although that seems like overkill (at least for this example) when you … Read more

How to mix HTML and C# Code in MVC3 with Razor?

Let’s start with improving your code. Improvement step 1: @foreach(var myObj in Model.Select((model, index) => new { model, index })) { <div class=”class@(myObj.index % 2 == 0 ? “1” : “2”)”> @Html.Partial(“_Foo”, myObj.model) </div> } Improvement Step 2 (using a custom HTML helper for the class): @foreach(var myObj in Model.Select((model, index) => new { model, … Read more

error with decimal in mvc3 – the value is not valid for field

I just stumbled on this again after 2 years. I thought ASP.NET MVC 5 had solved this but looks like it’s not the case. So here goes how to solve the problem… Create a class called DecimalModelBinder like the following and add it to the root of your project for example: using System; using System.Globalization; … Read more

How to change Visual Studio 2012 Razor colors

Optional: Install the color theme editor: http://visualstudiogallery.msdn.microsoft.com/366ad100-0003-4c9a-81a8-337d4e7ace05 In the theme editor or Tools > Options > Environment > Fonts and Colors Search for: ‘HTML Server-Side Script’ to edit the foreground & background of @ and other tags. Search for: ‘Razor Code’ to edit the background of the Razor code.

Can’t get site root url in asp mvc

To get the current host with port (mysite.com, www.mysite.com or localhost:9876) Request.Url.Authority To get your current application folder: (/ or /appfolder/) Url.Content(“~/”) To mix them? String.Format(“{0}://{1}{2}”,Request.Url.Scheme, Request.Url.Authority,Url.Content(“~/”)) OR (As torm pointed out) Url.Action(“”, null, null, Request.Url.Scheme) Url.Action(“”, null, null, “http”) Url.Action(“”, null, null, “https”) To generate an Action URL: Url.Action(“About”,”Home”,null,”http”)

@Html.DisplayNameFor for details model

I found there was no need for helper methods, extra code or looping through the collection. I just used the following: @Html.DisplayNameFor(model => model.Carriers.FirstOrDefault().Name) This still works even if FirstOrDefault() would return null because it’s only looking for meta data, the Name property itself is not accessed. Many thanks to @Kurian who inspired this answer.

VS 2013 giving The operation could not be completed. Invalid pointer on Razor views

For anyone arriving here after installing Visual Studio 2015 Update 1 and then updating some packages, here is what fixed it for me: Close Visual Studio Delete %LocalAppData%\Microsoft\VisualStudio\14.0\ComponentModelCache Re-open Visual Studio I was getting this error message in VS2015 and the fix worked for me, so it seems to apply to 2013 and 2015. I … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)