我正在尝试将 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 的示例,但需要采用其他方式。
任何帮助,将不胜感激。