I would like to be able to choose which properties is added to my object before using it in my action method helper. So I used an ExpandoObject to achieve this.
dynamic routeValues = new ExpandoObject();
if (...) { routeValues.FirstParam = "one"; }
if (...) { routeValues.SecondParam = "two"; }
helper.Action("MyAction", "MyController", routeValues);
It compiled succes开发者_运维技巧sfully but at runtimes the routeValues object seems to be ignored.
Any solution on dynamically choosing properties of an object ?
You don't need any dynamics or ExpandoObjects, a simple RouteValueDictionary will do the job:
var routeValues = new RouteValueDictionary();
if (...) { routeValues["FirstParam"] = "one"; }
if (...) { routeValues["SecondParam"] = "two"; }
helper.Action("MyAction", "MyController", routeValues);
精彩评论