开发者

ExpandoObject not accepted for routeValues in Action method?

开发者 https://www.devze.com 2023-04-06 04:37 出处:网络
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.

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消