【问题标题】:create a json and post it to action method and use the data + MVC4 + WCF创建一个 json 并将其发布到操作方法并使用数据 + MVC4 + WCF
【发布时间】:2013-12-26 12:41:33
【问题描述】:

我创建了如下 JSON 字符串并将其发布到我的控制器。而且我也在使用 WCF 服务

var inputParams = { 类型:“xxx”, 名称:“yyy” };

    var url = '@Url.Action("Action", "Controller")?inputParams=' + JSON.stringify(inputParams);

控制器

动态值 = WCFServiceClient.GetValues(jSerializer.Serialize(inputParams));

服务

动态输入参数 = jSerializer.Deserialize(inputParams);

这一点我得到的结果是:- {"Type":"Student","Name":"Ann"}

我将如何获得“类型”和“名称”的值?

【问题讨论】:

    标签: asp.net-mvc json wcf


    【解决方案1】:

    像这样创建模型类

    Public class YourModel 
    {
    Public string type{get;set;}
    Public string name{get;set;}
    
    }
    

    // 在你的行动中

    Public ActionResult yourAction(YourModel Model)
    {
    // Here You Can Access Your Values 
    //Like 
     Model.Type or Model.Name 
    
    
    
    }
    

    注意:记住一件事你的模型属性名称应该与传入的 stringyfy 数组匹配

    【讨论】:

    • 我尝试使用“YourModel Model”作为参数,而不是“inputParams”作为我的 Action 方法的参数。但是我的模型的所有属性都为 NULL。另一个问题是如果我这样做,我需要再次构造 JSON 字符串以传递给 WCF 服务?请指教。
    猜你喜欢
    • 2016-03-22
    • 1970-01-01
    • 2012-01-15
    • 2012-04-26
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多