How can dynamic breadcrumbs be achieved with ASP.net MVC?

Sitemap’s are definitely one way to go… alternatively, you can write one yourself! (of course as long as standard MVC rules are followed)… I just wrote one, I figured I would share here. @Html.ActionLink(“Home”, “Index”, “Home”) @if(ViewContext.RouteData.Values[“controller”].ToString() != “Home”) { @:> @Html.ActionLink(ViewContext.RouteData.Values[“controller”].ToString(), “Index”, ViewContext.RouteData.Values[“controller”].ToString()) } @if(ViewContext.RouteData.Values[“action”].ToString() != “Index”){ @:> @Html.ActionLink(ViewContext.RouteData.Values[“action”].ToString(), ViewContext.RouteData.Values[“action”].ToString(), ViewContext.RouteData.Values[“controller”].ToString()) } Hopefully someone … Read more

ASP.NET MVC Pass object from Custom Action Filter to Action

The better approach is described by Phil Haack. Basically this is what you do: public class AddActionParameterAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); // Create integer parameter. filterContext.ActionParameters[“number”] = 123; // Create object parameter. filterContext.ActionParameters[“person”] = new Person(“John”, “Smith”); } } The only gotcha is that if you are creating object … Read more

Having issue with multiple controllers of the same name in my project

The error message contains the recommended solution: “If this is the case, register this route by calling an overload of the ‘MapRoute’ method that takes a ‘namespaces’ parameter.” routes.MapRoute( “Default”, // Route name “{controller}/{action}/{id}”, // URL with parameters new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }, // Parameter defaults new string[] … Read more

Visual Studio 2013 – No Visual Basic/Visual C# Web Templates Installed

I think that “Re-install Visual Studio from scratch” is not a solution. I have faced with the described problem and found much faster way to fix it: First of all, try to repair Visual Studio installation (in “Control Panel\Programs\Programs and Features” find your Visual Studio, right-click and select “Repair”). Reboot after (!). Check if template … Read more

MVC3 Validation – Require One From Group

Here’s one way to proceed (there are other ways, I am just illustrating one that would match your view model as is): [AttributeUsage(AttributeTargets.Property)] public class RequireAtLeastOneOfGroupAttribute: ValidationAttribute, IClientValidatable { public RequireAtLeastOneOfGroupAttribute(string groupName) { ErrorMessage = string.Format(“You must select at least one value from group \”{0}\””, groupName); GroupName = groupName; } public string GroupName { get; … Read more