Can be used to serialize an object to a QueryString.
public static class HtmlHelper
{
public static string SerializeToQueryString(object source)
{
if (source == null)
throw new ArgumentNullException("source");
var type = source.GetType();
var queryString = new StringBuilder();
foreach (var property in type.GetProperties())
{
var propertyValue = property.GetValue(source, new object[] {});
if (propertyValue == null)
continue;
queryString.Append(HttpUtility.UrlEncode(property.Name));
queryString.Append("=");
queryString.Append(HttpUtility.UrlEncode(propertyValue.ToString()));
queryString.Append("&");
}
if (queryString.Length > 0)
queryString.Remove(queryString.Length - 1, 1);
return queryString.ToString();
}
}
/invoice/search/?" + HtmlHelper.SerializeToQueryString(searchInput)
Fork
Fork of Serialize an object into a Querystring, using LINQ syntax - @benlaan Thursday 17, 2011 6:03 AM
0 Feedback
You must log in before you can give any feedback
You must log in before you can post a comment