Get [DisplayName] attribute of a property in strongly-typed way

There are two ways to do this: Models.Test test = new Models.Test(); string DisplayName = test.GetDisplayName(t => t.Name); string DisplayName = Helpers.GetDisplayName<Models.Test>(t => t.Name); The first one works by virtue of writing a generic extension method to any TModel (which is all types). This means it will be available on any object and not just … Read more

Asp.net mvc override OnException in base controller keeps propagating to Application_Error

The following should work: protected override void OnException(ExceptionContext filterContext) { if (filterContext.ExceptionHandled) { return; } filterContext.Result = new ViewResult { ViewName = “~/Views/Shared/Error.aspx” }; filterContext.ExceptionHandled = true; } Also make sure that no exception is thrown in this method or it will propagate to Application_Error.

ASP.NET MVC Html.DropDownList populated by Ajax call to controller?

In the editor template provide an empty dropdown: <%= Html.DropDownListFor( x => x.PropertyToHoldSelectedValue, Enumerable.Empty<SelectListItem>(), “– Loading Values –“, new { id = “foo” }) %> Then setup a controller action that will return the values: public class FooController: Controller { public ActionResult Index() { return Json(new[] { new { Id = 1, Value = “value … Read more

Asynchronous Controller is blocking requests in ASP.NET MVC through jQuery

There are two issues in play here. The first is that your controller is not truly asynchronous. Spinning up a ThreadPool thread to perform work generally has worse performance characteristics than just doing everything from within the action method itself, as you’re still taking ThreadPool resources from ASP.NET (which just shares the CLR ThreadPool), and … Read more

Firefox 6 Infinite Page Refresh With Page With Hash Tags

Turns out, this is an issue with an old version of MicrosoftAjax.js (the one that comes installed with Asp.Net MVC 2). Open up the MicrosoftAjax.debug.js file and check the file version number. The top of this file will look like this if this is your problem: // Name: MicrosoftAjax.debug.js // Assembly: System.Web.Extensions // Version: 4.0.0.0 … Read more

Need an ASP.NET MVC long running process with user feedback

Here’s a sample I wrote that you could try: Controller: public class HomeController : AsyncController { public ActionResult Index() { return View(); } public void SomeTaskAsync(int id) { AsyncManager.OutstandingOperations.Increment(); Task.Factory.StartNew(taskId => { for (int i = 0; i < 100; i++) { Thread.Sleep(200); HttpContext.Application[“task” + taskId] = i; } var result = “result”; AsyncManager.OutstandingOperations.Decrement(); AsyncManager.Parameters[“result”] … Read more

ModelState.AddModelError – How can I add an error that isn’t for a property?

I eventually stumbled upon an example of the usage I was looking for – to assign an error to the Model in general, rather than one of it’s properties, as usual you call: ModelState.AddModelError(string key, string errorMessage); but use an empty string for the key: ModelState.AddModelError(string.Empty, “There is something wrong with Foo.”); The error message … Read more

Are

the colon syntax means you’ll be html encoded automatically: http://haacked.com/archive/2009/09/25/html-encoding-code-nuggets.aspx They couldn’t just html encode all the existing <%= blocks, because things that are already properly encoded (which is hopefully most of the projects out there) would look strange.