How to Display HTML with MVC’s NullDisplayText Attribute

Adding DisplayFormat attribute to model’s property allows you to set formatting options for that property’s value. One of options is NullDisplayText, where you can set what to display if the value is null.

Sometimes you may wish to have NullDisplayText return a piece of HTML. To be able to render this HTML correctly, you need to set HtmlEncode value to false.

[DisplayFormat(NullDisplayText = "<div>My HTML<div>", HtmlEncode = false)]
public int? MyProperty { get; set; }

Hide URL Path When Printing in Chrome

When you print (or print preview) web page in Chrome, printout will include URLs for each link on the page.

For example, if you have a link “Back to Homepage”, when printed it will display as “Back to Homepage (”.

To stop URLs being printed, add the following style –

 <style media="print">
        content:none !important;

ASP.NET MVC – Prevent Browser Back Button Openning Page After Logout

If you have ASP.NET MVC application with login / logout functionality (for example using Forms Authentication), you may have noticed that after logout, if you hit back button in your browser, last page you were on will open. In secure application this could present a problem, as it could expose information that only authorized users should view.

Culprit in this scenario is our browser, as it will quite happily show cached version of the page after you’ve logged out.

There isn’t much you can do, except tell your browser to not cache your application’s pages. You can do this for individual pages in your applications, or if you want all pages not to be cached, you can add an event in Global.asax file.

Add Application_BeginRequest() method to your application’s Global.asax file, and add code below which will disable browser caching for each request.

        protected void Application_BeginRequest()

The ‘System.Web.Security. SqlMembershipProvider’ requires a database schema compatible with schema version ‘1’

I had a situation recently where I’ve added ASP.Net membership to a development database as part of requirements for a new project. After implementation was finished and tested in development environment, I used Red Gate’s SQL Compare to update test database with ASP.Net membership objects (since I had some other database changes that I wanted to deploy at the same time). After finishing test deployment, I tried to create a new user, and got a message – The ‘System.Web.Security.SqlMembershipProvider’ requires a database schema compatible with schema version ‘1’.

The problem was that, whilst ASP.Net membership schema and object were created, aspnet_SchemaVersions table was not populated. So I copied data from this table that was in development database to test database, and the problem was solved (I did have to restart IIS on test server as well). So If you are creating ASP.Net membership object via comparison tool (instead of using asp_regsql), make sure to populate aspnet_SchemaVersions table as well.

Asp.Net – “Sys.ArgumentNullException: Value cannot be null. Parameter name: elements” Error when using AjaxControlToolkit

Today I started experiencing a weird problem with AjaxControlToolkit Calendar Extender control – it would render at the top left corner of the page, and not under the text box control it was bound to.

Fired up Google Developer Tools and saw an error that was being thrown – Sys.ArgumentNullException: Value cannot be null. Parameter name: elements.

Adding ScriptMode=”Release” to asp:ToolkitScriptManager solved the issue. Not sure why release version of the script libraries would be different to debug version (or why debug version causes the problem), but this solved my problem.

ASP.Net – How to set value of date textbox via server side code

If you are using textbox with TextMode=”Date”, you need to format the date string before setting value via server side code.

Use ToString(“yyyy-MM-dd”) to format the date value before assigning it to the text box.

txtEOIStart.Text = DateTime.Now.ToString("yyyy-MM-dd");

ASP.Net CheckBoxList client side JavaScript validation

If you need to validate CheckBoxList on client side using JavaScript, here is a way to do it. This code will check that at least one checkbox is selected, return true if it is and false if there are no selections.

function Validate() {
     //Other control validation goes here...
     var buSelected = false;
     var relevantBUs = document.getElementById("cblBusinessUnits");

     var businessUnit = relevantBUs.getElementsByTagName("input");
     for (var i = 0; i < businessUnit.length; i++) {
          if (businessUnit[i].checked) {
               buSelected = true;

     if (!buSelected) {
          alert("Please select at least one option.");
          return false;
     return true;