You can use HTTP post as the following code to do it, you just creat a class with Key and Value, and use it in Controller with a String variable, on a HTTP post Function.
I am not expert in C# I did translate online from my VB code that work well.
In your Views (.cshtml) :
<input id="Button1" type="button" value="button" onclick="SaveMe()" />
<script>
function SaveMe() {
var GUID = '6a580cf1-2f05-4621-8a67-8fe0bdd559c2';
// Creat Object
var lineItems = new Object();
lineItems.Entrys = new Array();
// Example Filling your object, but of course you can get data from another place ...
lineItems.Entrys[0] = new Object({ Key: '052c5941-233a-4bd2-80e1-7cfffa34ca44', Value: 'Salary1' });
lineItems.Entrys[1] = new Object({ Key: '00000000-0000-0000-0000-000000000004', Value: 'Salary2' });
lineItems.Entrys[2] = new Object({ Key: '00000000-0000-0000-0000-000000000005', Value: 'Salary3' });
$.ajax({
type: "POST",
url: "/Home/AjaxMethodDigestFile",
data: JSON.stringify({ Entrys: lineItems.Entrys, GUID: GUID }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) { alert(response.message); },
failure: function (response) { alert("failure"); },
error: function (response) { alert("error"); }
});
}
</script>
In your Models:
namespace MyModel1.ViewModel
{
public class MyClass1
{
public string Key { get; set; }
public string Value { get; set; }
}
}
In your Controllers:
[HttpPost]
public JsonResult AjaxMethodDigestFile(ICollection<MyModel1.ViewModel.MyClass1> Entrys, string GUID)
{
string message = "";
int counter = 0;
foreach (var entry in Entrys)
{
// How to use this data example
string Key = entry.Key;
string Value = entry.Value;
counter += 1;
message += Value + ": " + Key + Constants.vbCrLf;
}
// The following lines are not necessary, it's just an example code to know what happen and return it to client side ...
if (counter > 0)
message = counter.ToString() + " Entrys received" + Constants.vbCrLf + message;
else
message = "no entrys";
var response = new { counter, message };
return Json(response);
}