我想在同一个控制器的Action结果中发布控制器名称 Home 的Mvc Action结果,Post请求发送成功,但参数丢失了传递值。
从控制器主页调用帖子
[ValidateInput(false)]
[HttpPost]
public ActionResult addToWishList(string customcode, string addcusimgSVG, string pagestatus = "", string userinformation="")
{
string URI = "http://localhost:49389/services";
string myParameters = "serviceAndVisitorModel=" + ServiceAndVisitroData;
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string HtmlResult = wc.UploadString(URI, myParameters);
}
//return some view here
}
由同一控制器中的addToWishList调用的操作结果
[Route("services")]
[HttpPost]
public ActionResult services(AllserviceWebApi serviceAndVisitorModel, string pagename = "service")//string selectedService,string userId,string DealInfo
{
//serviceAndVisitorModel is null here
}
模型
public class AllserviceWebApi
{
public string packageTypeID { get; set; }
public string packageTypeCode { get; set; }
public string pageUrl { get; set; }
public string pageName { get; set; }
public string displayName { get; set; }
public string name { get; set; }
public List<pakageInfoList> packageInfoList { get; set; }
//------visitor object
public string IPAddress { get; set; }
public string deviceName { get; set; }
public string OSName { get; set; }
public string browserName { get; set; }
public string countryName { get; set; }
public string selectedService{ get; set; }
public string userId{ get; set; }
public string OrderId { get; set; }
public string DealInfo { get; set; }
public long wishlishid { get; set; }
public string customlogoCode { get; set; }
public string customLogoImgSvg { get; set; }
public bool IsCustomllogo { get; set; }
}
模型初始化
AllserviceWebApi ServiceAndVisitroData = new AllserviceWebApi()
{
selectedService = serviceids,
userId = "0",
OrderId = "0",
DealInfo = "",
IPAddress = ipaddress,
// deviceName: userDevicedata[0],
OSName = osandversion,
browserName = browsermajorversion,
wishlishid = wishid,
customlogoCode = logocode,
customLogoImgSvg = logosvg,
IsCustomllogo = true
};
请告诉我这段代码有什么问题。
答案 0 :(得分:1)
尝试通过JSON发送
using Newtonsoft.Json;
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/json";
string HtmlResult = wc.UploadString(URI, JsonConvert.SerializeObject(ServiceAndVisitroData));
}
PS你需要通过nuget安装Newtonsoft.Json