我想將FormCollection傳遞給我的ASP.NET MVC控制器並將其轉換爲動態對象,然後將其序列化爲Json並傳遞給我的Web API。將NameValueCollection轉換爲動態對象
[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"];
}
我見過的例子動態對象轉換成字典或NameValueValueCollection,但需要走另一條路。
任何幫助,將不勝感激。
你能說你想達到什麼嗎? –
我猜你是要求兩行代碼將集合變成ExpandoObject? –
你在找這樣的事嗎? http://stackoverflow.com/a/7596697/187697 – keyboardP