在 Dotnet Core MVC 中填充下拉列表 - 从动态 JSON 产品 URL 解析
Populate Dropdownlist in Dotnet Core MVC - Parse from dynamic JSON Product URL
示例 JSON 文件:
{ "products" : [{ "Name": "Cheese", "Price" : 2.50, "Location": "Refrigerated foods"},{ "Name": "Crisps", "Price" : 3, "Location": "the Snack isle"}, { "Name": "Pizza", "Price" : 4, "Location": "Refrigerated foods"}, { "Name": "Chocolate", "Price" : 1.50, "Location": "the Snack isle"}, { "Name": "Self-raising flour", "Price" : 1.50, "Location": "Home baking"}, { "Name": "Ground almonds", "Price" : 3, "Location": "Home baking"}]}
Controller.cs代码
public IActionResult Index()
{
var webClient = new WebClient();
//var jsonUrlProducts = webClient.DownloadString(@"https://raw.githubusercontent.com/mdn/fetch-examples/master/fetch-json/products.json");
var jsonUrlProducts = webClient.DownloadString(@"C:\Users\NatarajanS\source\repos\KC_EC_WebSite_1\KC_EC_WebSite_1\wwwroot\lib\JSON\Json_F3.json");
var dictProducts = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonUrlProducts);
ViewData["selectedProductTextDict"] = dictProducts;
return View();
}
Index.cshtml代码:
@Html.DropDownList("ddlProductByText",
new SelectList((System.Collections.IEnumerable)ViewData["selectedProductTextDict"], "Value", "Key"))
{ "products" : [{ "Name": "Cheese", "Price" : 2.50, "Location": "Refrigerated foods"}] }
我需要用对象键名称填充我的下拉列表。上面的示例 JSON 文件 .. 名称、价格、位置
但是上面的代码只填充了我的根对象名称 products。
我认为您必须创建 class 对象并使用它,如下面的代码:
public class Product {
public string Name { get; set; }
public double Price { get; set; }
public string Location { get; set; }
}
它帮助你获得所有道具。
Dropdownlist values 正如 Atabai 所说,您需要创建一个模型来映射 json.For 您的 json,模型设计应该是:
public class Test
{
public List<Product> products { get; set; }
}
public class Product
{
public string Name { get; set; }
public float Price { get; set; }
public string Location { get; set; }
}
控制器:
[HttpGet]
public ActionResult Index()
{
//....
var dictProducts = JsonConvert.DeserializeObject<Test>(jsonUrlProducts);
ViewData["selectedProductTextDict"] = dictProducts.products;
return View();
}
查看(例如填写名单):
@Html.DropDownList("ddlProductByText",
new SelectList((System.Collections.IEnumerable)ViewData["selectedProductTextDict"], "Name", "Name"))
结果:
更新:
如果您只想显示名称、价格、位置,请检查以下代码:
[HttpGet]
public ActionResult Index()
{
//...
var dictProducts = JsonConvert.DeserializeObject<Dictionary<string,object>>(jsonUrlProducts);
var data =(JArray)dictProducts["products"];
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (JObject obj in data.Children())
{
foreach (JProperty prop in obj.Children())
{
string key = prop.Name.ToString();
string value = prop.Value.ToString();
dic.Add(key, value);
}
break;
}
ViewData["selectedProductTextDict"] = dic;
return View();
}
查看:
@Html.DropDownList("ddlProductByText",
new SelectList((System.Collections.IEnumerable)ViewData["selectedProductTextDict"], "Value", "Key"))
结果:
示例 JSON 文件:
{ "products" : [{ "Name": "Cheese", "Price" : 2.50, "Location": "Refrigerated foods"},{ "Name": "Crisps", "Price" : 3, "Location": "the Snack isle"}, { "Name": "Pizza", "Price" : 4, "Location": "Refrigerated foods"}, { "Name": "Chocolate", "Price" : 1.50, "Location": "the Snack isle"}, { "Name": "Self-raising flour", "Price" : 1.50, "Location": "Home baking"}, { "Name": "Ground almonds", "Price" : 3, "Location": "Home baking"}]}
Controller.cs代码
public IActionResult Index()
{
var webClient = new WebClient();
//var jsonUrlProducts = webClient.DownloadString(@"https://raw.githubusercontent.com/mdn/fetch-examples/master/fetch-json/products.json");
var jsonUrlProducts = webClient.DownloadString(@"C:\Users\NatarajanS\source\repos\KC_EC_WebSite_1\KC_EC_WebSite_1\wwwroot\lib\JSON\Json_F3.json");
var dictProducts = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonUrlProducts);
ViewData["selectedProductTextDict"] = dictProducts;
return View();
}
Index.cshtml代码:
@Html.DropDownList("ddlProductByText",
new SelectList((System.Collections.IEnumerable)ViewData["selectedProductTextDict"], "Value", "Key"))
{ "products" : [{ "Name": "Cheese", "Price" : 2.50, "Location": "Refrigerated foods"}] }
我需要用对象键名称填充我的下拉列表。上面的示例 JSON 文件 .. 名称、价格、位置
但是上面的代码只填充了我的根对象名称 products。
我认为您必须创建 class 对象并使用它,如下面的代码:
public class Product {
public string Name { get; set; }
public double Price { get; set; }
public string Location { get; set; }
}
它帮助你获得所有道具。
Dropdownlist values 正如 Atabai 所说,您需要创建一个模型来映射 json.For 您的 json,模型设计应该是:
public class Test
{
public List<Product> products { get; set; }
}
public class Product
{
public string Name { get; set; }
public float Price { get; set; }
public string Location { get; set; }
}
控制器:
[HttpGet]
public ActionResult Index()
{
//....
var dictProducts = JsonConvert.DeserializeObject<Test>(jsonUrlProducts);
ViewData["selectedProductTextDict"] = dictProducts.products;
return View();
}
查看(例如填写名单):
@Html.DropDownList("ddlProductByText",
new SelectList((System.Collections.IEnumerable)ViewData["selectedProductTextDict"], "Name", "Name"))
结果:
更新:
如果您只想显示名称、价格、位置,请检查以下代码:
[HttpGet]
public ActionResult Index()
{
//...
var dictProducts = JsonConvert.DeserializeObject<Dictionary<string,object>>(jsonUrlProducts);
var data =(JArray)dictProducts["products"];
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (JObject obj in data.Children())
{
foreach (JProperty prop in obj.Children())
{
string key = prop.Name.ToString();
string value = prop.Value.ToString();
dic.Add(key, value);
}
break;
}
ViewData["selectedProductTextDict"] = dic;
return View();
}
查看:
@Html.DropDownList("ddlProductByText",
new SelectList((System.Collections.IEnumerable)ViewData["selectedProductTextDict"], "Value", "Key"))
结果: