gpt4 book ai didi

c# - 将 NameValueCollection 转换为动态对象

转载 作者:太空狗 更新时间:2023-10-30 00:41:56 26 4
gpt4 key购买 nike

<分区>

我正在尝试获取传递到我的 ASP.NET MVC Controller 的 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 的示例,但需要采用其他方式。

如有任何帮助,我们将不胜感激。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com