简体   繁体   English

如何使用angularjs MVC C#将参数作为对象传递?

[英]how to pass parameter as a object using angularjs mvc c#?

i want to pass pass value as a object using angularjs with ajax call.i have create one class in c#.and now i am call service in angularjs and pass object but controller side i am not getting this value. 我想使用带有ajax调用的angularjs将传递值作为对象传递给我。我已经在c#中创建了一个类。现在我在angularjs中调用服务并传递对象,但是控制器端却没有得到这个值。

i have create this class in c#: 我已经在C#中创建了此类:

 public class FType
 {
    public string Type { get; set; }
    public string Way { get; set; }
 }

i have write this code for call controller.js : 我已经为call controller.js编写了以下代码:

var Params = {
        Type: 'All',           
        Way: ''
    };
$scope.dtOptions = Userservice.GetAllUserss(DTOptionsBuilder, Params)

this is my service.js code: 这是我的service.js代码:

this.GetAllUserss = function (DTOptionsBuilder, Params) {        
    var response = DTOptionsBuilder.newOptions().withOption("ajax", {
        dataSrc: "data",
        url: "/admin/getuserlist",
        type: "POST",
        data: Params            
    });
    return response;
};

this is my c# method: 这是我的C#方法:

[HttpPost]
    [Route("getuserlist")]
    [AllowAnonymous]
    public ActionResult getuserlist(FType T) // here i am always getting null
    {
    }

any one how can getting this value from c# side please let me know. 任何人如何从C#端获取此值,请告诉我。

    this.GetAllUserss = function (DTOptionsBuilder, Params) {        
    var response = DTOptionsBuilder.newOptions().withOption("ajax", {
        dataSrc: "data",
        url: "/admin/getuserlist?T=",
        type: "POST",
        data: JSON.Stringify(Params)            
    });
    return response;
};

public ActionResult getuserlist(string T)
{

}

Try this you must be getting the data in the service, then deserialize it to the object. 尝试此操作,您必须在服务中获取数据,然后将其反序列化为对象。

You need to stringify the data to a json string, the MVC default json serializer will take care of the rest 您需要将数据字符串化为json字符串,MVC默认的json序列化程序将负责其余的工作

Try it like this: 像这样尝试:

this.GetAllUserss = function (DTOptionsBuilder, Params) {        
    var response = DTOptionsBuilder.newOptions().withOption("ajax", {
        dataSrc: "data",
        url: "/admin/getuserlist",
        type: "POST",
        data: JSON.Stringify(Params) 
    });
    return response;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM