ASP.NET MVC – Combine Json result with ViewResult

You can also render a PartialViewResult to a string, and then pass this string via JSON to your view, rendering it in your page using jQuery. You can see that in this post: http://www.atlanticbt.com/blog/asp-net-mvc-using-ajax-json-and-partialviews/. I’ve created an extension to make it easier: public static class MvcHelpers { public static string RenderPartialView(this Controller controller, string viewName, … Read more

How do I unit test a custom ActionFilter in ASP.Net MVC

You just need to test the filter itself. Just create an instance and call the OnActionExecuted() method with test data then check the result. It helps to pull the code apart as much as possible. Most of the heavy lifting is done inside the CsvResult class which can be tested individually. You don’t need to … Read more

Asp.Net MVC Themes, how to?

Here’s my code that I’ve been using for implementing “Themes” in ASP.NET MVC: ASP.NET MVC: Implement Theme Folders using a Custom ViewEngine It’s nice when you’re able to just swap out CSS files to change the theme, but this really isn’t very practical in a ton of cases. Especially when each theme needs to have … Read more

Prevent IIS from serving static files through ASP.NET pipeline

I’m taking a guess here and suspect that you have the following setting configured in your web.config file: <modules runAllManagedModulesForAllRequests=”true”> This means that every request, including those for static content is hitting the pipeline. Change this setting to: <modules runAllManagedModulesForAllRequests=”false”> This is assuming your application is running under ASP.NET 4.0 and MVC3. For this to … Read more

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

ASP.NET MVC Programmatically Get a List of Controllers

Using Jon’s suggestion of reflecting through the assembly, here is a snippet you may find useful: using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Web.Mvc; public class MvcHelper { private static List<Type> GetSubClasses<T>() { return Assembly.GetCallingAssembly().GetTypes().Where( type => type.IsSubclassOf(typeof(T))).ToList(); } public List<string> GetControllerNames() { List<string> controllerNames = new List<string>(); GetSubClasses<Controller>().ForEach( type => controllerNames.Add(type.Name)); return … 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

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