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

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”)