Repro:<br /><br />- Create an Action method as follows that displays a WebGrid with paging enabled:<br /><br /> public class MyController : Controller<br /> {<br /> ...<br /> public ActionResult MyAction(IList<int> productId)<br /> {<br /> .... return a View that contains a WebGrid with paging or sorting enabled<br /> }<br /> }<br /><br />- Navigate to the action with the following URL:<br /><br /> http://servername/MyController/MyAction?productId=1&productId=3&productId=5<br /><br />- Examine the paging or sorting urls in the rendered WebGrid<br /><br />Expected result:<br /><br />- Generated urls should contain the same repeated keys as the original url, e.g.:<br /><br /> http://servername/MyController/MyAction?productId=1&productId=3&productId=5&page=2<br /><br />Actual result:<br /><br />- Generated urls replace repeated querystring keys by a single key with a comma-separated value, e.g.:<br /><br /> http://servername/MyController/MyAction?productId=1%2c3%2c5&page=2<br /> <br />- As a consequence, the productId parameter of the Action method received a single value "1,3,5" instead of a list of values.<br /><br />Analysis:<br /><br />This is a bug in the method WebGrid.GetPath, which does not take account of repeated values in the QueryString NameValueCollection. To fix it, replace the following code:<br /><br /> for (int i = 0; i < queryString.Count; i++)<br /> {<br /> if (i > 0)<br /> {<br /> sb.Append("&");<br /> }<br /> sb.Append(HttpUtility.UrlEncode(queryString.Keys[i]));<br /> sb.Append("=");<br /> sb.Append(HttpUtility.UrlEncode(queryString[i]));<br /> }<br /><br />with something like:<br /><br /> char separator = '?';<br /> for (int i = 0; i < queryString.Count; i++)<br /> {<br /> string encodedKey = HttpUtility.UrlEncode(queryString.Keys[i]);<br /> foreach(string value in queryString.GetValues(i))<br /> {<br /> sb.Append(separator);<br /> separator = '&';<br /> sb.Append(encodedKey);<br /> sb.Append("=");<br /> sb.Append(HttpUtility.UrlEncode(value));<br /> }<br /> }<br />
↧