How to simulate Server.Transfer in ASP.NET MVC?

How about a TransferResult class? (based on Stans answer) /// <summary> /// Transfers execution to the supplied url. /// </summary> public class TransferResult : ActionResult { public string Url { get; private set; } public TransferResult(string url) { this.Url = url; } public override void ExecuteResult(ControllerContext context) { if (context == null) throw new ArgumentNullException(“context”); … Read more

How to cache data in a MVC application

Here’s a nice and simple cache helper class/service I use: using System.Runtime.Caching; public class InMemoryCache: ICacheService { public T GetOrSet<T>(string cacheKey, Func<T> getItemCallback) where T : class { T item = MemoryCache.Default.Get(cacheKey) as T; if (item == null) { item = getItemCallback(); MemoryCache.Default.Add(cacheKey, item, DateTime.Now.AddMinutes(10)); } return item; } } interface ICacheService { T GetOrSet<T>(string … Read more

getting the values from a nested complex object that is passed to a partial view

You can pass the prefix to the partial using @Html.Partial(“MyPartialView”, Model.ComplexModel, new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = “ComplexModel” }}) which will perpend the prefix to you controls name attribute so that <input name=”Name” ../> will become <input name=”ComplexModel.Name” ../> and correctly bind to typeof MyViewModel on post back Edit To make … Read more

What are the main disadvantages of Java Server Faces 2.0?

JSF 2.0 disadvantages? Honestly, apart from the relative steep learning curve when you don’t have a solid background knowledge about basic Web Development (HTML/CSS/JS, server side versus client side, etc) and the basic Java Servlet API (request/response/session, forwarding/redirecting, etc), no serious disadvantages comes to mind. JSF in its current release still needs to get rid … Read more

MVC5 – How to set “selectedValue” in DropDownListFor Html helper

When you use the DropDownListFor() (or DropDownList()) method to bind to a model property, its the value of the property that sets the selected option. Internally, the methods generate their own IEnumerable<SelectListItem> and set the Selected property based on the value of the property, and therefore setting the Selected property in your code is ignored. … Read more

Why map special routes first before common routes in asp.net mvc?

The routing engine will take the first route that matches the supplied URL and attempt to use the route values in that route. The reason why this happens is because the RouteTable is used like a switch-case statement. Picture the following: int caseSwitch = 1; switch (caseSwitch) { case 1: Console.WriteLine(“Case 1”); break; case 1: … Read more