title: Replace Placeholders in Html with Model Object
body: <p>I have a hard time explaining what I'm exactly trying to do. There's probably a name for it, but I don't know what it is.
  First, I have a model such as:</p>
  
  <pre><code>   public class Customer
     {
       public int Id { get; set; }
       public int ProductId { get; set; }
       ...more properties...
  
       public virtual Product Product { get; set; }
     }
     public class Product
     {
       public int Id { get; set; }
       public string Name { get; set; }
       public string Description { get; set; }
       ...more properties...
  
     }
  </code></pre>
  
  <p>Second, I have a string of HTML text with placeholders in <code>{}</code>. I'd like to have something like <code>{Id}</code> and have it replace the Html text with the model properties.</p>
  
  <blockquote>
    <p>&lt;div&gt;&lt;span&gt;Name&lt;/span&gt;&lt;span&gt;{Id}-{Product.Name}&lt;/span&gt;&lt;/div&gt;</p>
  </blockquote>
  
  <p>My thought was to use a NameValueCollection to get the Model properties as strings. Using reflection, I can do that for the base properties, but not for something like <code>Product.Name</code>.</p>
  
  <p>Am I going about this the wrong way? What could I use to get a NameValueCollection that I could loop through and do a replace of the Html?</p>
  
  <p>Here is the current code I have (skips virtual properties): </p>
  
  <pre><code>public virtual NameValueCollection GetNameValueCollection(Object obj)
  {
    Type type = obj.GetType();
    PropertyInfo[] properties = type.GetProperties();
    var coll = new NameValueCollection();
    foreach (PropertyInfo property in properties)
    {
      if(!property.GetGetMethod().IsVirtual)
      {
        if (property.PropertyType == typeof(DateTime))
        {
          var date = (DateTime)property.GetValue(obj, null);
          coll.Add(property.Name, date.ToLongDateString());
        }
        else if (property.PropertyType == typeof(DateTime?))
        {
          var date = (DateTime?)property.GetValue(obj, null);
          if (date.HasValue)
          {
            coll.Add(property.Name, date.Value.ToLongDateString());
          }
          else
          {
            coll.Add(property.Name, string.Empty);
          }
        }
        else
        {
          var value = property.GetValue(obj, null);
          if (value != null)
          {
            coll.Add(property.Name, value.ToString());
          }
        }
      }
    }
    return coll;
  }
  </code></pre>
  
  <p>This should be recursive, but it seems like there should be a better way. By the way, I don't need a <code>NameValueCollection</code> specifically (could be <code>Dictionary&lt;string,string&gt;</code> for example). Thoughts? Is there a nuget package that already does this?</p>