各位先進好:
問題主要架構如下圖1,2所示,HTML頁面上有多組資料集,之前的寫法為先
取出Type A資料集裡的Group1和Group2的Array集合,呼叫API對應到ASP.NET MVC架構寫入資料庫,寫入成功後再呼叫一次API
取出Type B資料集裡的Group1和Group2的Array集合,再一次呼叫API對應到ASP.NET MVC架構寫入資料庫,寫入成功後再一次呼叫API,
然後再取出Type C資料集裡的Group1和Group2的Array集合帶入API。
這樣寫法的問題是,假如在Type B資料集寫入時發生錯誤,資料將rollback,但前面的A資料集己經寫入成功了,所以之前A的資料集就無法rollback了。
[圖1. HTML架構與Js取API架構]
圖1. 架構範例
[圖2. ASP.NET MVC接收資料架構]
圖2. 架構範例
[請問前輩們的圖片都是放到那個空間再貼到IT邦的呢?放google無法直接秀圖呀><]
所以想要改寫為先將所有要寫入的資料一次打包起來(目前想到的是JSON格式),一次帶入API跑廻圈一次寫入,其中若發生錯誤則全部資料都要Rollback.
目前的寫法JS是Group1與Group2資料集各別取出Array帶入,ASP.NET MVC則用List g1, List g2來接應至相對應的Model來寫入.
1.如果要改寫,那JS這邊一大包的資料該怎麼寫才能與ASP.NET MVC的接口接應呢?
2.如果改用Json的方式來打包,那ASP.NET MVC那邊接到後要怎麼拆解才能重新對應到Group1 Model與Group2 Model裡呢?
3.Json的部份打包成下列這樣可以嗎?用陣列來包物件~
{
"TypeA":
[
"ModelGroup1": Group1,
"ModelGroup2": Group2
],
"TypeB":
[
"ModelGroup1": Group1,
"ModelGroup2": Group2
],
"TypeC":
[
"ModelGroup1": Group1,
"ModelGroup2": Group2
]
}
我查了javascript Array轉JSON的範例,轉完之後還是Array呀,也沒有變成Json,javascript Array可以轉成JSON格式嗎?
var arr = [ "John", "Peter", "Sally", "Jane" ];
var myJSON = JSON.stringify(arr);
先感謝大大們指教了~
TypeA B C資料集弄成一包呼一次
改成這樣試試看 我沒有測試過 若有錯誤再提供錯誤訊息看看
JS
let typeAData = [{ ModelGroup1 : "Group1", ModelGroup2 : "Group2"}];
let typeBData = [{ ModelGroup1 : "Group1", ModelGroup2 : "Group2"}];
let typeCData = [{ ModelGroup1 : "Group1", ModelGroup2 : "Group2"}];
$.ajax({
url: url,
data: {
TypeA : typeAData,
TypeB : typeBData,
TypeC : typeCData
},
success: function(res){
}});
C#
public class ModelGroup
{
public ModelGroup1 ModelGroup1 { get; set; }
public ModelGroup2 ModelGroup2 { get; set; }
}
public class NewClass
{
public List<ModelGroup> TypeA { get; set; }
public List<ModelGroup> TypeB { get; set; }
public List<ModelGroup> TypeC { get; set; }
}
[HttpPost]
public ActionResult Insert(NewClass newclass)
{
using (DB1Entities db1 = new DB1Entities())
{
using (var t1 = db1.Database.BeginTransaction())
{
try
{
// insert 資料
// ...
db1.SaveChanges();
t1.Commit();
}
catch (Exception ex)
{
t1.Rollback();
}
}
}
return Json(new { Message = "Success"});
}
參考您的架構,我改寫我想要的架構如下:
public class InsertGroupA
{
public string paramA1 { get; set; }
public string paramA2 { get; set; }
}
public class InsertGroupB
{
public string paramB1 { get; set; }
public string paramB2 { get; set; }
}
public class JsonPostModel
{
List<InsertGroupA> GroupA { get; set; }
List<InsertGroupB> GroupB { get; set; }
public string Type { get; set; }
public string Product { get; set; }
}
let GroupA = [{
paramA1: 'paramA1',
paramA2: 'paramA2'
},{ paramA1: 'paramA1',
paramA2: 'paramA2'
}];
let GroupB = [{
paramB1: 'paramB1',
paramB2: 'paramB2'
},{ paramB1: 'paramB1',
paramB2: 'paramB2'
}];
//3組測試集,內含兩個GroupA和GroupB集合
let MultiJson2 = {
formPosts: [{
Type: "TypeA",
Product: "ProductA",
GroupA: GroupA,
GroupB: GroupB
}, {
Type: "TypeB",
Product: "ProductB",
GroupA: GroupA,
GroupB: GroupB
}, {
Type: "TypeC",
Product: "ProductC",
GroupA: GroupA,
GroupB: GroupB
}
]
};
$.ajax({
type: "POST",
url: "@Url.Action("JqueryPost")",
data: JSON.stringify(MultiJson2),
contentType: "application/json",
async: false,
success: function (result) {
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
[HttpPost]
public ActionResult JqueryPost(List<JsonPostModel> formPosts)
{
return View("Create");
}
送出後3組測試的LIST值有拿到,Product和Type值也都有拿到,但內層的LIST的值為null,也就是物件裡的GroupA和GroupB的List清單拿不到,不知道該如何可以拿到LIST和LIST的值呢?
如果只寫一個階層,LIST的值是可以取到的,如下例
let model = [{
Title: 'TestTitleAA',
UserName: 'TestContentA',
Description: "DescriptionA"
}, {
Title: 'TestTitleBB',
UserName: 'TestContentB',
Description: "DescriptionB"
}, {
Title: 'TestTitleCC',
UserName: 'TestContentC',
Description: "DescriptionC"
}];
let commentData = [];
commentData.push({ Subject: "subjectA", Body: "bodyA", UserName: "UserNameA" });
commentData.push({ Subject: "subjectB", Body: "bodyB", UserName: "UserNameB" });
let json = {
type: "typeOK",
model: model,
comment: commentData
};
$.ajax({
type: "POST",
url: "@Url.Action("JqueryPost")",
data: JSON.stringify(json),
contentType: "application/json",
async: false,
success: function (result) {
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
[HttpPost]
public ActionResult JqueryPost(string type, List<Photo> model, List<Comment> comment)
{
return View("Create");
}