一、传变量
客户端请求
客户端一般是用Jscript写的,传送变量,代码如下:
javascript
function save(i){
//这是用Post Method 使用var1=value1&var2=value2格式传送数据
var count=chkCount.value;
var chk0="";
for (k=0;k<count;k++){
var chkBox=document.getElementById("c_"+k.toString());
if (chkBox.checked){
chk0+=chkBox.value+",";
}
}
var s0 = "/Store/Save" + i.toString() ;
xhr0.open("POST", s0, true);
xhr0.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr0.send(`chk0=${chk0}`);
}
服务器端
按变量进行接收,并已解析完毕。
csharp
public async Task<JsonResult> Save1(string chk0)
{
int n = 0;
string[] sa=chk0.Split(",");
List<string> chk =new List<string>(sa);
chk.RemoveAt(chk.Count-1); */
if (chk0.Length > 0)
{
n = await SaveChk(chk0, 1);
}
return RedirectToAction(nameof(Warehousing));
}
二、传Json数据
客户端
传送Json流数据。
javascript
var count=chkCount.value;
var chk0=[];
var chkBox;
for(n=0;n<count;n++){
chkBox=document.getElementById("c_"+n.toString());
if (chkBox.checked){
chk0[chk0.length]=chkBox.value;
}
}
var s0 = "/Store/Save" + i.toString() ;
xhr0.open("POST", s0, true);
xhr0.setRequestHeader("Content-type","application/Json");//"application/x-www-form-urlencoded");// ;
xhr0.send(JSON.stringify(chk0));
服务器端
服务器端,对Json数据是按流来处理的,所以就必须从流中读取,并自动进行解析。
csharp
public async Task<JsonResult> Save1()
{
string[] chk = await Request.ReadFromJsonAsync<string[]>(); ;
if (chk.Length > 0)
{
n = await SaveChk(chk, 1);
}
int status = 0;
int flowId = 3;
List<WaybillView> waybillViews = GetWaybillViews(status, flowId);
return Json(waybillViews);
}