使用的model 1
{
public class AreaModel : BaseModelVM
{
public bool status { get; set; } //API回傳狀態,是否成功
public List<AreaDataItem>? data { get; set; }
}
public class AreaDataItem
{
public string? id { get; set; }
public string? name { get; set; }
public string? kindId { get; set; }
public string? sort { get; set; }
public string? isDelete { get; set; }
public string? createDT { get; set; }
public string? editDT { get; set; }
public object? deleteDT { get; set; }
}
AreaDataItem資料來源
public class GetAreaApi
{
public static AreaModel demoJson() //這個有抓到API!!!!!!!!!!
{
using (WebClient wc = new WebClient())
{
string strURI = "";
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string area_data = wc.UploadString(strURI, "POST"); //api字串儲存在area_data
AreaModel re_data = new AreaModel()
{
status = true,
data = new List<AreaDataItem>(),
};
re_data = JsonSerializer.Deserialize<AreaModel>(area_data); //單筆資料的JSON反序列化List
var List = re_data;
return re_data;
}
}
}
model 2
public class WarehouseModel : BaseModelVM
{
public bool status { get; set; }
public List<WarehouseDataItem> data { get; set; }
}
public class WarehouseDataItem
{
public string id { get; set; }
public string name { get; set; }
public string eareaid { get; set; }
public string kindid { get; set; }
public string warehouseNo { get; set; }
public string isDelete { get; set; }
public string createDT { get; set; }
public string editDT { get; set; }
public object deleteDT { get; set; }
}
WarehouseDataItem資料來源
public class GetWarehouseApi
{
public static WarehouseModel demoJson()
{
using (WebClient wc1 = new WebClient())
{
string strURI1 = "";
wc1.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string warehouse_data = wc1.UploadString(strURI1, "POST"); //api字串儲存在warehouse_data
WarehouseModel re_dataw = new WarehouseModel()
{
status = true,
data = new List<WarehouseDataItem>()
};
re_dataw = JsonSerializer.Deserialize<WarehouseModel>(warehouse_data); //單筆資料的JSON反序列化List
var List = re_dataw;
return re_dataw;
}
}
}
統整上述兩個Model的BaseModelVM
public class BaseModelVM
{
public AreaModel Area { get; set; } //AreaModel的數據
public WarehouseModel Warehouse { get; set; } //Warehouse的數據
}
VIEW的部分
@using Banana.Models;
@model Banana.Models.BaseModelVM
@{
Layout = "_Layout";
}
div class="text-center">
<br />
@if (Model.Area != null)
{
@Model.Area.status <br />
if (Model.Area != null)
{
foreach (var item in Model.Area.data)
@item.name <br/>
}
}
<br />
@if (Model.Warehouse != null)
{
@Model.Warehouse.status <br />
if (Model.Warehouse.data != null)
{
foreach (var item in Model.Warehouse.data)
@item.name <br/>
}
}
</div>
Controller的部分
public IActionResult Index2()
{
BaseModelVM vm = new BaseModelVM();
vm.Area = new AreaModel().Area;
vm.Warehouse = new WarehouseModel().Warehouse;
return View(vm);
}
最後跑出來會變這樣
想請問是哪裡出了問題呢
類似 然後上面兩個model的AreaDataItem 跟 WarehouseDataItem的資料是由呼叫API數據得來的
public static AreaModel demoJson() //這個有抓到API!!!!!!!!!!
{
using (WebClient wc = new WebClient())
{
string strURI = "";
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string area_data = wc.UploadString(strURI, "POST"); //api字串儲存在area_data
AreaModel re_data = new AreaModel()
{
status = true,
data = new List<AreaDataItem>(),
};
re_data = JsonSerializer.Deserialize<AreaModel>(area_data); //單筆資料的JSON反序列化List
var List = re_data;
return re_data;
}
}
所以還是不太知道要用甚麼方法才寫得出來
public class WarehouseDataItem
{
public string id { get; set; }
public string name { get; set; }
public string eareaid { get; set; }
public string kindid { get; set; }
public string warehouseNo { get; set; }
public string isDelete { get; set; }
public string createDT { get; set; }
public string editDT { get; set; }
public object deleteDT { get; set; }
}
public class AreaDataItem
{
public string id { get; set; }
public string name { get; set; }
public string kindId { get; set; }
public string sort { get; set; }
public string isDelete { get; set; }
public string createDT { get; set; }
public string editDT { get; set; }
public object deleteDT { get; set; }
}
public class BaseModelVM
{
public List<AreaDataItem> AreaDatas { get; set; }
public List<WarehouseDataItem> WarehouseDatas { get; set; }
}
public IActionResult Index2()
{
return View(new BaseModelVM{
AreaDatas = GetAreaApi.demoJson(),
WarehouseDatas = GetWarehouseApi.demoJson()
});
}
public class GetWarehouseApi
{
public static List<WarehouseDataItem> demoJson()
{
using (WebClient wc1 = new WebClient())
{
string strURI1 = "";
wc1.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string warehouse_data = wc1.UploadString(strURI1, "POST");
if(string.IsNullOrEmpty(warehouse_data))
{
return null;
}
return JsonSerializer.Deserialize<List<WarehouseDataItem>>(warehouse_data);
}
}
}
public class GetAreaApi
{
public static List<AreaDataItem> demoJson()
{
using (WebClient wc = new WebClient())
{
string strURI = "";
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string area_data = wc.UploadString(strURI, "POST");
if(string.IsNullOrEmpty(area_data))
{
return null;
}
return JsonSerializer.Deserialize<List<AreaDataItem>>(area_data);
}
}
}
-----------------------------------------------------------------------------
@using Banana.Models;
@model Banana.Models.BaseModelVM
@{
Layout = "_Layout";
}
<div class="text-center">
<br />
@if (Model.AreaDatas != null)
{
foreach (AreaDataItem item in Model.AreaDatas)
{
@item.name <br/>
}
}
<br />
@if (Model.WarehouseDatas != null)
{
foreach (WarehouseDataItem item in Model.WarehouseDatas)
{
@item.name <br/>
}
}
</div>
沒測試過,僅針對有疑問的地方提供修改方案。
原本看到標題是想請你直接使用TypeBuilder去寫,但看了一下發現你的問題跟需求可能沾不上邊。
您好 我有試出來了~
不過我BaseModelVM還是維持
public AreaModel Area { get; set; } //AreaModel的數據
public WarehouseModel Warehouse { get; set; } //Warehouse的數據
然後想請教您Controller那邊的寫法
return View
(
new BaseModelVM
{
Area = GetAreaApi.demoJson(),
Warehouse = GetWarehouseApi.demoJson()
}
);
為什麼要這樣寫呢 小妹我是程式新手所以希望能搞懂每一句的意思 非常感謝您!!!
BaseModelVM baseModelVM = new BaseModelVM();
baseModelVM.Area = GetAreaApi.demoJson();
baseModelVM.Warehouse = GetWarehouseApi.demoJson();
return View(baseModelVM);
BaseModelVM baseModelVM =
new BaseModelVM
{
Area = GetAreaApi.demoJson(),
Warehouse = GetWarehouseApi.demoJson()
};
return View(baseModelVM);
return View(
new BaseModelVM
{
Area = GetAreaApi.demoJson(),
Warehouse = GetWarehouseApi.demoJson()
});
算是簡寫