Saturday, August 27, 2011

must have the get with the set when binding to a model on [HttpPost]

It turns out that a get/setter must have the get half of the equation (the get) for the settings to work upon model binding by post-to-action means. The get has to return something or else the set fails to set. Note the extraneous getters in this class:

using System;

namespace MyApp.Helpers

{

   public class SearchParametersForFoo

   {

      public AttributeNormalizationLayer Parameters = new AttributeNormalizationLayer();

      

      public string Name

      {

         
get { return ""; }

         set {

            Parameters.Append("Name", value);

         }

      }

      

      public Int32 Number

      {

         
get { return 0; }

         set {

            Parameters.Append("Number", value);

         }

      }

   }

}

 
 

It would be nice if I could just leave out the lines in black above, but if I do this binding won't work.

[HttpPost]

public ActionResult Index(SearchParametersForFoo searchParametersForFoo)

{

   Hashtable hashTable = searchParametersForFoo.Parameters.Collection;

   
more code follows...

 
 

As it is, what I’m attempting is really kludgy. I'm trying to spool up a bunch of parameters related to filtering down a list of Foo into a Hashtable for handing to another class that will run a query based upon what is in the Hashtable which serves as a universal container for search parameters. Maybe this class should be a base class instead of a parameter on my Foo-specific object. I'm still working that out.

using System.Collections;

namespace MyApp.Helpers

{

   public class AttributeNormalizationLayer

   {

      public Hashtable Collection = new Hashtable();

      

      public void Append(string name, object value)

      {

         Collection.Add(name,value.ToString());

      }

   }

}

 
 

Or, well, honestly. I don't think there is any ambiguity. It should be a base class.

What am I trying to do? We need to be able to filter/search against a number of domain objects. (Many of these objects will have custom attributes in addition to static attributes.) We need one way of doing it and I am going to need to be able to hand to an API, that Kar-Khan will expose, a Hashtable of items to include in a query. The Hashtable will have an <A,B> format for WHERE A like 'B' flavored searching. Joel's whiteboarding from yesterday gives a high-level view here:

Addendum 8/22/2015: I am commenting out http://a.yfrog.com/img615/1945/t494.jpg which yfrog has seen fit to replace with some sort of iTunes advertisement. I wish I had not started up my blog hosting images with these clowns.

No comments:

Post a Comment