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

How does Html.Raw MVC helper work?

Because encoded characters are HTML, and the Raw version of that string is the encoded one. Html.Raw renders what it is given without doing any html encoding, so with ViewBag.div = “<div> Hello </div>”;: @Html.Raw(ViewBag.div); Renders <div> Hello </div> However, when you have encoded characters in there, such as ViewBag.Something = “&gt;”; the raw version … 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 } );

ASP.NET MVC Model Binder for Generic Type

Create a modelbinder, override BindModel, check the type and do what you need to do public class MyModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (HasGenericTypeBase(bindingContext.ModelType, typeof(MyType<>)) { // do your thing } return base.BindModel(controllerContext, bindingContext); } } Set your model binder to the default in the global.asax protected void … Read more

Post JavaScript array with AJAX to asp.net MVC controller

You could define a view model: public class AddUserViewModel { public int ProjectId { get; set; } public int[] userAccountIds { get; set; } } then adapt your controller action to take this view model as parameter: [HttpPost] public ActionResult AddUsers(AddUserViewModel model) { … } and finally invoke it: function sendForm(projectId, target) { $.ajax({ url: … Read more

There is no ViewData item of type ‘IEnumerable’ that has the key country

If you were using DropDownListFor like this: @Html.DropDownListFor(m => m.SelectedItemId, Model.MySelectList) where MySelectList in the model was a property of type SelectList, this error could be thrown if the property was null. Avoid this by simply initializing it in constructor, like this: public MyModel() { MySelectList = new SelectList(new List<string>()); // empty list of anything… … Read more