Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8216787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:15:13+00:00 2026-06-07T12:15:13+00:00

I’ve got a POCO that I’m using as an argument to an action in

  • 0

I’ve got a POCO that I’m using as an argument to an action in MVC3. Something like this:

My Type

public class SearchData
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }
}

My Action

public ActionResult Index(SearchData query)
{
    // I'd like to be able to do this
    if (query == null)
    {
        // do something
    }
}

Currently, query is passed as an instance of SearchData with all of the properties as null. I’d prefer that i get a null for query so I can just do the null check that I have in the above code.

I could always look at ModelBinder.Any() or just the various keys in ModelBinder to see if it got any of the properties for query, but I don’t want to have to use reflection to loop over the properties of query. Also, I can only use the ModelBinder.Any() check if query is my only parameter. As soon as I add additional parameters, that functionality breaks.

With the current model binding functionality in MVC3, is it possible to get the behavior of returning null for POCO argument to an action?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:15:14+00:00Added an answer on June 7, 2026 at 12:15 pm

    You’ll need to implement a custom modelbinder to do this. You can just extend DefaultModelBinder.

    public override object BindModel(
        ControllerContext controllerContext, 
        ModelBindingContext bindingContext)
    {
        object model = base.BindModel(controllerContext, bindingCOntext);
        if (/* test for empty properties, or some other state */)
        {
            return null;
        }
    
        return model;
    }
    

    Specific Implementation

    This is the actual implementation of the binder that will return null for the model if all of the properties are null.

    /// <summary>
    /// Model binder that will return null if all of the properties on a bound model come back as null
    /// It inherits from DefaultModelBinder because it uses the default model binding functionality.
    /// This implementation also needs to specifically have IModelBinder on it too, otherwise it wont get picked up as a Binder
    /// </summary>
    public class SearchDataModelBinder : DefaultModelBinder, IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            // use the default model binding functionality to build a model, we'll look at each property below
            object model = base.BindModel(controllerContext, bindingContext);
    
            // loop through every property for the model in the metadata
            foreach (ModelMetadata property in bindingContext.PropertyMetadata.Values)
            {
                // get the value of this property on the model
                var value = bindingContext.ModelType.GetProperty(property.PropertyName).GetValue(model, null);
    
                // if any property is not null, then we will want the model that the default model binder created
                if (value != null)
                    return model;
            }
    
            // if we're here then there were either no properties or the properties were all null
            return null;
        }
    }
    

    Adding this as a binder in global.asax

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        ModelBinders.Binders.Add(typeof(SearchData), new SearchDataModelBinder());
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    
        MvcHandler.DisableMvcResponseHeader = true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am doing a simple coin flipping experiment for class that involves flipping a
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.