Conditional validation on model in MVC

If you’re on MVC3/.NET4, you can use IValidatableObject which exists specifically for such purposes. Quoting ScottGu, …The IValidatableObject interface enables you to perform model-level validation, and enables you to provide validation error messages specific to the state of the overall model…. You model would look like public class MyViewModel : IValidatableObject { public long? Id … Read more

Enable / disable session state per controller / action method

This is now moved from Futures into MVC3. There’s a ControllerSessionState attribute (apparently will be named SessionState for the final release of MVC3), which can be applied to a controller, something like this: [SessionState(SessionStateBehavior.Disabled)] public class MyController : Controller { … (But in the RC version, you must use ControllerSessionState

Routing with and without controller name in ASP.NET MVC 4

This should do it: routes.MapRoute( name: “About”, url: “About”, defaults: new { controller = “Public”, action = “About” } ); routes.MapRoute( name: “MyPageSummary”, url: “MyPage”, defaults: new { controller = “MyPage”, action = “Summary” } ); routes.MapRoute( name: “Default”, url: “{controller}/{action}/{id}”, defaults: new { controller = “Public”, action = “Start”, id = UrlParameter.Optional } );

How can I create a route constraint of type System.Guid?

Create a RouteConstraint like the following: public class GuidConstraint : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { if (values.ContainsKey(parameterName)) { string stringValue = values[parameterName] as string; if (!string.IsNullOrEmpty(stringValue)) { Guid guidValue; return Guid.TryParse(stringValue, out guidValue) && (guidValue != Guid.Empty); } } return false; }} Next when adding … Read more

ASP.NET MVC – Pass array object as a route value within Html.ActionLink(…)

Try creating a RouteValueDictionary holding your values. You’ll have to give each entry a different key. <% var rv = new RouteValueDictionary(); var strings = GetStringArray(); for (int i = 0; i < strings.Length; ++i) { rv[“str[” + i + “]”] = strings[i]; } %> <%= Html.ActionLink( “Link”, “Action”, “Controller”, rv, null ) %> will … Read more

How to use a ViewBag to create a dropdownlist?

You cannot used the Helper @Html.DropdownListFor, because the first parameter was not correct, change your helper to: @Html.DropDownList(“accountid”, new SelectList(ViewBag.Accounts, “AccountID”, “AccountName”)) @Html.DropDownListFor receive in the first parameters a lambda expression in all overloads and is used to create strongly typed dropdowns. Here’s the documentation If your View it’s strongly typed to some Model you … Read more

Html.Raw() in ASP.NET MVC Razor view

Html.Raw() returns IHtmlString, not the ordinary string. So, you cannot write them in opposite sides of : operator. Remove that .ToString() calling @{int count = 0;} @foreach (var item in Model.Resources) { @(count <= 3 ? Html.Raw(“<div class=\”resource-row\”>”): Html.Raw(“”)) // some code @(count <= 3 ? Html.Raw(“</div>”) : Html.Raw(“”)) @(count++) } By the way, returning … Read more