Tips in ASP.NET MVC

Enabling and Disabling Client-Side Validation Globally

From Config
<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>

From Code
protected void Application_Start()
{
//Enable or Disable Client Side Validation at Application Level
HtmlHelper.ClientValidationEnabled = true;
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
}

Disable validation for a single field in client side

@Html.TexBoxFor(model => model.SomeValue,                  
new Dictionary<string, object> { { "data-val", false }}) 

Enabling and Disabling Client-Side Validation for Specific View

@model MvcApp.Models.SampleModel
@{
ViewBag.Title = "Home";
HtmlHelper.ClientValidationEnabled = false;
}

Enjoy reading

Change Response From Xml to Json in ASP.NET MVC WepApi

By default in ASP.NET MVC WepApi  the response format of Restful methods is XML. You can change this to JSON by simply removing the current response format like below in WepApiConfig

public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

//Code for removing the current response format
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
}
}

Enjoy coding..