【发布时间】:2013-08-03 00:49:59
【问题描述】:
我正在尝试将 FormCollection 传递给我的 ASP.NET MVC 控制器并将其转换为动态对象,然后将其序列化为 Json 并传递给我的 Web API。
[HttpPost]
public ActionResult Create(FormCollection form)
{
var api = new MyApiClient(new MyApiClientSettings());
dynamic data = new ExpandoObject();
this.CopyProperties(form, data); // I would like to replace this with just converting the NameValueCollection to a dynamic
var result = api.Post("customer", data);
if (result.Success)
return RedirectToAction("Index", "Customer", new { id = result.Response.CustomerId });
ViewBag.Result = result;
return View();
}
private void CopyProperties(NameValueCollection source, dynamic destination)
{
destination.Name = source["Name"];
destination.ReferenceCode = source["ReferenceCode"];
}
我见过将动态对象转换为 Dictionary 或 NameValueValueCollection 的示例,但需要采用其他方式。
任何帮助将不胜感激。
【问题讨论】:
-
你能说一下你想达到什么目标吗?
-
我猜你是在要求两行代码将集合变成一个 ExpandoObject?
-
您在寻找这样的东西吗? stackoverflow.com/a/7596697/187697
-
我试图在stackoverflow.com/questions/43858023/…回答你的问题
标签: c# asp.net-mvc