1

How can I find out in a custom ModelBinder in ASP.NET MVC whether I am binding to a parameter that has a default value or not?

Default value:

public void Show(Ship ship = null)
{
     // ...
}

No default value:

public void Show(Ship ship)
{
     // ...
}

ModelBinder:

public class ModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var modelType = bindingContext.ModelType;

        // Is it an item from the database?
        if (typeof(IDbObject).IsAssignableFrom(modelType))
        {
            // Get from database...
            var result = BindValue();

            if (result == null && NotOptional()) // Code for NotOptional needed
                throw new Exception();

            return result;
        }
    }
}

I want to know this because I want to show an error message if a user does a request to an action and does not provide all necessary information (which would be all parameters that have no default value).

Dennis
  • 1,027
  • 1
  • 14
  • 30
  • 1
    if (ship == null) { // no information } else { // has information } , does work i think ? what is issue here? – A.T. Jul 14 '14 at 10:12
  • Can you show the code where you want to know this? When does your binder call a method on your model? – CodeCaster Jul 14 '14 at 10:28
  • @Aaron: The check should be performed automatically in the modelbinder so that we do not have to check in each action method. – Dennis Jul 14 '14 at 10:33
  • @CodeCaster: Updated question to include example code. – Dennis Jul 14 '14 at 10:41
  • _What_ do you want to test for optional there? – CodeCaster Jul 14 '14 at 10:41
  • I want to test which arguments are optional and which not. So in this case I want to know at model binding time whether `ship` is optional or not. – Dennis Jul 14 '14 at 10:47

2 Answers2

0

I don't think there is any efficient or reasonable way to tell if a method input parameter has a default value. If you're looking for a way to ensure the incoming data is proper, you would want to bind the view form fields to a model and use ModelState.IsValid to test if all fields have data.

A great introduction can be found here: http://www.codeproject.com/Articles/710776/Introduction-to-ASP-NET-MVC-Model-Binding-An-Absol

Josh
  • 547
  • 1
  • 7
  • 21
0

If I understand correctly, you can query whether the parameter is decorated with OptionalAttribute

var method = typeof(YourClassName).GetMethod("Show");
foreach (var pi in method.GetParameters())
{
    var optionalAttribute = pi.GetCustomAttributes<OptionalAttribute>().FirstOrDefault();
    if (optionalAttribute != null)
    {
        //This is optional parameter
        object defaultValue = pi.DefaultValue;
    }
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • Are there any performance issues using reflection like this in a model binder? – Dennis Jul 14 '14 at 10:29
  • I don't know mvc, so I can't answer your question till you can say how frequently it will be called? See [How costly is Reflection?](http://stackoverflow.com/questions/25458/how-costly-is-net-reflection?lq=1) – Sriram Sakthivel Jul 14 '14 at 10:35