Set Web.config to Show Error Messages

In ASP.NET web application development it is common practice to deploy to staging server before deploying it to production environment. Sometimes it is useful to show errors on staging environment and even production environment, especially after realizing that there is no errors on development environment. Most errors could be related to enviroment such as not being able to connect to database, IIS security issue, etc.

Here is how to add or change Web.config of the deployed application on staging server. Use Notepad or Notepad++ to open root Web.config, find or add the following xml tags:

To disable or hide error messages again, you can simply comment it out like below:

 

Using EnumDropDownListFor HTML Helper

This is to explain EnumDropDownListFor HTML helper behaviour in ASP.NET MVC 5 with example code. Please remember, EnumDropDownListFor is not available in earlier version MVC, i.e. ASP.NET MVC 4 or before.

Default EnumDropDownListFor Behaviour

First, let’s say you want to create a drop down list from below employment type enum, then in your model you can create public non-nullable property of the enum type, see below code:

Then in EmployeeController Create action:

Now in your view inside html form, you can simply use EnumDropDownListFor HTML helper like this:

The HTML helper EnumDropDownListFor will generate drop down list with the following items:

  • ToBeDetermined (value = 0, selected)
  • FullTime (value = 1)
  • PartTime (value = 2)

Specify Default Selected Item

By default, if no selected item specified, enum item of value 0 will be selected. But for very common reason if you need to set a default value for the drop down list, you can create a new view model object in the controller action first, then set inital value for the property of type enum and pass the newly created model object to the razor view. For example in EmployeeController Create action:

This time EnumDropDownListFor will generate drop down list with the following items:

  • ToBeDetermined (value = 0)
  • FullTime (value = 1, selected)
  • PartTime (value = 2)

Enum Does Not Have Item of Value 0

What happens if you explicitly specify enum item values that start from 1 like this:

By default, if there enum member of value 0 does not exist, EnumDropDownListFor will automatically create a HTML drop down list with a prompt, i.e. a empty text select list item with item value of 0.

So, EnumDropDownListFor will generate drop down list with the following items:

  • [Empty Text] (value = 0, selected)
  • FullTime (value = 1)
  • PartTime (value = 2)

But if you specify default value for the property such as in Create action example above, a prompt item will not be generated, and you will have drop down list with only the following items:

  • FullTime (value = 1, selected)
  • PartTime (value = 2)