Want to know if ValueInjecter can help me

May 28, 2010 at 12:00 PM
Edited May 28, 2010 at 12:13 PM

Hi,

In my MVC project I'm not using Html.DropDownList but other third-party controls. I am getting all Raddiobuttons, Checkboxes and Comboboxes values via Request. All other form controls come updated in my model from view.

I would like to use ViewModel pattern. Could ValueInjecter helps me in order to get these controls values in some automatic way?

May I create my own ValueInjector for get these Request values?

Thanks in advance,

Kings.

Coordinator
May 28, 2010 at 2:02 PM
Edited Jun 10, 2010 at 2:04 PM

Hi Kings,

ValueInjecter can do anything, even I don't know yet all the possibilities :),

anyways, I think you need something like this:

 

public class RequestInjection: KnownSourceValueInjection<HttpRequestBase>
    {
        protected override void Inject(HttpRequestBase source, object target, PropertyDescriptorCollection targetProps)
        {
            for (var i = 0; i < targetProps.Count; i++)
            {
                var activeTarget = targetProps[i];
                var value = source[activeTarget.Name];
                if (value == null) continue;

                //you got the value, do something with it

                //I'm setting the value here directly but it's very likely that you might need some preprocessing, 
                //I recommend you to debug this stuff
                activeTarget.SetValue(target, value);
            }
        }
    }

and use it like this:
o.InjectFrom<RequestInjection>(Request);

Regards,

Omu

 

Coordinator
Jun 10, 2010 at 2:05 PM

UPDATE: There is a sample asp.net-mvc application in the download which demonstrates how to do this stuff and much more

Cheers

Omu

 

Jun 10, 2010 at 2:48 PM
Edited Jun 10, 2010 at 5:15 PM

Thank you.

ValueInjector is the most!!

So now, may I use SameNameType to inject values from/to my Entities/ViewModels?

Or as shown in ViewModelToEntitySampleController can I inject from one to another directly?

Best regards,

Kings.

Coordinator
Jun 11, 2010 at 5:39 AM

:) you can inject from anything to anything, that's the main idea,

SameNameType is now LoopValueInjection, and you can extend it to change it's behavior (add prefix, change the value before setting it), and this is the default injection now, so doing:

o.InjectFrom(foo)

it's the same as writing

o.InjectFrom<LoopValueInjection>(foo)

Cheers

Omu

Oct 26, 2010 at 11:12 AM

Hi again,

I'm using version 2.0 of ValueInjector.

In my RequestInjection class I'm having an error when I try to convert my ComboBox integer selected value via request parameters (then, it is a string value) to a Nullable<int>.

Here is my code:

        protected override void Inject(HttpRequestBase request, object target, PropertyDescriptorCollection targetProps)
        {
            string[] requestKeys = request.Params.AllKeys;

            for (var i = 0; i < targetProps.Count; i++)
            {
                object value = null;
                var activeTarget = targetProps[i];
                // get bool values
                if (new[] { typeof(bool), typeof(Nullable<bool>) }
                    .Contains(activeTarget.PropertyType))
                {
                    value = (request[activeTarget.Name] == activeTarget.Name);
                }
                // get combo values
                else
                {
                    string activeTargetName = string.Concat(activeTarget.Name, "_Value");
                    if (requestKeys.Contains(activeTargetName))
                    {
                        value = Convert.ChangeType(request[activeTargetName], activeTarget.PropertyType); // EXCEPTION HERE
                    }
                }
                if (value == null) continue;

                activeTarget.SetValue(target, value);
            }
        }

How can I convert a values to its type or Nullable<type> dynamically?

Thanks in advance,

Kings.

Oct 26, 2010 at 11:33 AM

Hi,

I could solve this problem in this way:

                        Type conversionType = activeTarget.PropertyType;
                        if (conversionType.IsGenericType &&
                            conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                        {
                            if (request[activeTargetName] == null)
                                value = null;

                            NullableConverter nullableConverter = new NullableConverter(conversionType);
                            conversionType = nullableConverter.UnderlyingType;

                        }

                        value = Convert.ChangeType(request[activeTargetName], conversionType);

Best regards,

Kings.

Coordinator
Oct 26, 2010 at 2:14 PM

Hi,

cool :), thnx for solving this without me :)

Cheers,

Omu