Some notes on software development... RSS 2.0
# Wednesday, 06 June 2012

This post is an extension to How to Bind to Enums on JSON Objects in ASP.NET MVC 3 from www.codetunnel.com so please see that excellent post for more details.
This class basically extends the MVC default model binder to include Enum property support. This allows models with enum properties to be correctly set (bound) when JSON is sent to the controllers action method.

The class is nearly identical to codetunnel’s version, I just included the Enum.Parse statement so that it also supports enum name values being passed in.

 

So the class looks like this:

    public class DefaultModelBinderEx : DefaultModelBinder
    {
        /// <summary>
        /// Fix for the default model binder's failure to decode enum types when binding to JSON.
        /// </summary>
        protected override object GetPropertyValue(
            ControllerContext controllerContext,
            ModelBindingContext bindingContext,
            PropertyDescriptor propertyDescriptor,
            IModelBinder propertyBinder)
        {
            var propertyType = propertyDescriptor.PropertyType;

            if (propertyType.IsEnum)
            {
                var providerValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

                if (providerValue != null)
                {
                    var value = providerValue.RawValue;

                    if (value != null)
                    {
                        var valueType = value.GetType();

                        if (valueType == typeof(string[]))
                        {
                            valueType = typeof(string);
                            value = ((string[])value)[0];
                        }

                        if (!valueType.IsEnum)
                        {
                            return Enum.Parse(propertyType, value.ToString());
                        }
                    }
                }
            }

            return base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
        }
    }

..and then you just need to update the Application_Start method in the Global.asax file with this line:

ModelBinders.Binders.DefaultBinder = new U413ModelBinder();

Now your default binding will support models with Enum properties.

Wednesday, 06 June 2012 13:20:35 (GMT Standard Time, UTC+00:00)  #    -
ASP.NET MVC
# Saturday, 24 December 2011

To compile MVC views at build time you can modify the MvcBuildViews option in the project file. To do this you have to manually edit your .proj file and setting MvcBuildViews=”true”. This gives you design time compilation of all your views so you can catch an silly view errors before running the website but the build time will be increased due to the extra compilation time.

However, when I set MvcBuildViews=”true” I immediately get the following build error:

It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS

The issue is caused by multiple web.config files in the obj folder. To fix this issue I added the BaseIntermediateOutputPath element to the .proj file. My .proj file now contains this:

<MvcBuildViews>true</MvcBuildViews>
<BaseIntermediateOutputPath>..\bin</BaseIntermediateOutputPath>

I also had to delete the contents of the existing obj folder.

Everything now builds and I get design time error checking of all the MVC views.

More information about this issue can be found here:
http://blogs.msdn.com/b/webdevtools/archive/2010/05/14/the-aspnet-compiler-build-task-in-visual-studio-2010-asp-net-mvc-2-projects.aspx

Saturday, 24 December 2011 19:09:41 (GMT Standard Time, UTC+00:00)  #    -
ASP.NET MVC | MSBuild
# Wednesday, 19 January 2011

When you install the microsoft-web-helpers package to a standard ASP.NET MVC 3 application it does not run first time due to some missing DLLs.

The install package:
image

The error:
image

To fix this error the project needs to reference the WebMatrix.Data.dll and WebMatrix.WebData.dll from the C:\Program Files\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies folder.

image

In Web.config add the following lines to the System.Web\Compilation\assemblies location:

<add assembly="WebMatrix.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="WebMatrix.WebData, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

Like so:
image

Running (F5) the basic MVC application with the microsoft-mvc-helpers package now works:

image

You can now use the Facebook, Twitter, Gravatar, Bing, Video, LinkShare etc helpers Smile

Wednesday, 19 January 2011 20:30:23 (GMT Standard Time, UTC+00:00)  #    -
ASP.NET MVC
Navigation

Sign In
Extras
Statistics
Total Posts: 116
This Year: 0
This Month: 0
This Week: 0
Comments: 36
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2017
Hadrian Phillips

All Content © 2017, Hadrian Phillips
DasBlog theme 'Business' created by Christoph De Baene (delarou)