【问题标题】:Parameter Lost Value When Post Action in Same Controller Asp.net Mvc在同一控制器 Asp.net Mvc 中发布操作时的参数丢失值
【发布时间】:2017-11-02 10:12:03
【问题描述】:

我只想在同一控制器中的动作结果中发布控制器名称 Home 中的 Mvc 动作结果,发布请求成功发送但参数丢失了传递值。

从控制器主页调用帖子

   [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

            };

请告诉我这段代码有什么问题。

【问题讨论】:

  • 你试过模型吗???
  • 什么是ServiceAndVisitroData? (AllserviceWebApi serviceAndVisitorModel 参数建议您要绑定到复杂对象 - 您不能将 string 绑定到复杂对象)
  • @StephenMuecke 请建议我如何绑定这个?
  • 我猜不出AllserviceWebApi是什么或者ServiceAndVisitroData的值是什么
  • @StephenMuecke 请考虑我编辑的问题

标签: c# asp.net asp.net-mvc asp.net-web-api http-post


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-26
    • 2011-03-15
    • 2016-05-28
    • 2014-04-04
    • 2011-02-14
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多