Issue #1297 - Null values in the ModelState throw off Validation Message Helper

Jun 30, 2009 at 4:54 PM
Edited Jun 30, 2009 at 4:56 PM

Hi,

I looked at the source code in RulesException, and found your workaround for Issue #1297.  After incorporating xVal into my project, I realized I was having a similar error due to a null value in the ModelState (this forum post explains my situation: http://forums.asp.net/p/1377232/2908213.aspx).  I was able to fix it by using SetModelValue(), instead of assigning a value to the existingValue object like you are doing now.  Do you think this approach would work for the original issue as well?  My workaround looks like this:

 

 public class RulesExceptionDecorator : RulesException
    {
        public RulesExceptionDecorator(IEnumerable<ErrorInfo> errors)
            : base(errors)
        {
        }

        public RulesExceptionDecorator(string propertyName, string errorMessage)
            : base(propertyName, errorMessage)
        {
        }

        public RulesExceptionDecorator(string propertyName, string errorMessage, object onObject)
            : base(propertyName, errorMessage, onObject)
        {
        }

        public new void AddModelStateErrors(ModelStateDictionary modelState, string prefix)
        {
            base.AddModelStateErrors(modelState, prefix);
            foreach (var error in Errors)
            {
                var key = string.IsNullOrEmpty(prefix) ? error.PropertyName : string.Format("{0}.{1}", prefix, error.PropertyName);

                ModelState existingValue;
                if (modelState.TryGetValue(key, out existingValue) && existingValue.Value == null)
                {
                    modelState.SetModelValue(key, new ValueProviderResult(null, string.Empty, CultureInfo.CurrentCulture));
                }
            }
        }
    }

 

 

Jul 5, 2009 at 1:11 PM

Hi greattall1,

Sorry about the delay in replying.

Have you got the latest ASP.NET MVC? I think the issue was fixed by the RC version and it’s definitely fixed in V1. Looks like it might work (apologies but I haven’t got time to work through it in detail.)

Your best bet is to talk directly to Steve Sanderson – he’s a very nice chap and usually responds quickly.

Rich

From: greattall1 [mailto:notifications@codeplex.com]
Sent: 30 June 2009 17:55
To: richard@eidetika.com
Subject: Issue #1297 - Null values in the ModelState throw off Validation Message Helper [xval:61090]

From: greattall1

Hi,

I checked out the source code in RuleException, and found your workaround for Issue #1297. After incorporating xVal into my project, I realized I was having a similar error due to a null value in the ModelState (this forum post explains my situation: http://forums.asp.net/p/1377232/2908213.aspx). I was able to fix it by using SetModelValue(), instead of assigning a value to the existingValue object like you are doing now. Do you think this approach would work for the original issue as well? My workaround looks like this:

 public class RulesExceptionDecorator : RulesException
    {
        public RulesExceptionDecorator(IEnumerable<ErrorInfo> errors)
            : base(errors)
        {
        }
 
        public RulesExceptionDecorator(string propertyName, string errorMessage)
            : base(propertyName, errorMessage)
        {
        }
 
        public RulesExceptionDecorator(string propertyName, string errorMessage, object onObject)
            : base(propertyName, errorMessage, onObject)
        {
        }
 
        public new void AddModelStateErrors(ModelStateDictionary modelState, string prefix)
        {
            base.AddModelStateErrors(modelState, prefix);
            foreach (var error in Errors)
            {
                var key = string.IsNullOrEmpty(prefix) ? error.PropertyName : string.Format("{0}.{1}", prefix, error.PropertyName);
 
                ModelState existingValue;
                if (modelState.TryGetValue(key, out existingValue) && existingValue.Value == null)
                {
                    modelState.SetModelValue(key, new ValueProviderResult(null, string.Empty, CultureInfo.CurrentCulture));
                }
            }
        }
    }

Read the full discussion online.

To add a post to this discussion, reply to this email (xval@discussions.codeplex.com)

To start a new discussion for this project, email xval@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com