ASP.NET MVC コントローラーに渡された FormCollection を動的オブジェクトに変換しようとしています。動的オブジェクトは 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 に変換する例を見てきましたが、別の方法が必要です。
どんな助けでも大歓迎です。