ASP.Net MVC RouteData and arrays

后端 未结 6 1764
遥遥无期
遥遥无期 2020-12-03 01:35

If I have an Action like this:

public ActionResult DoStuff(List stuff)
{
   ...
   ViewData[\"stuff\"] = stuff;
   ...
   return View();
}
         


        
6条回答
  •  一整个雨季
    2020-12-03 01:48

    Combining both methods works nicely.

    public static RouteValueDictionary FixListRouteDataValues(RouteValueDictionary routes)
    {
        var newRv = new RouteValueDictionary();
        foreach (var key in routes.Keys)
        {
            object value = routes[key];
            if (value is IEnumerable && !(value is string))
            {
                int index = 0;
                foreach (string val in (IEnumerable)value)
                {
                    newRv.Add(string.Format("{0}[{1}]", key, index), val);
                    index++;
                }
            }
            else
            {
                newRv.Add(key, value);
            }
        }
    
        return newRv;
    }
    

    Then use this method in any extension method that requires routeValues with IEnumerable(s) in it.

    Sadly, this workaround seams to be needed in MVC3 too.

提交回复
热议问题