繁体   English   中英

将对象从Angularjs传递到MVC控制器并映射到Class Object

[英]Pass an Object from Angularjs to MVC controller and map to Class Object

我在angularjs中有一个对象,我想要传递该对象并将其映射到mvc控制器中的自定义c#类。 但是每当我这样做时,该类对象就完全为null。

 $scope.Get = function () {
        var EService = [{
            id: $scope.Id,
            servicename: $scope.ServiceName,
            servicetype: $scope.ServiceType,
            monthlyrental: $scope.MonthlyRental,
            serviceremarks: $scope.ServiceRemarks,
            servicestatus: $scope.status,
            activationdate: $scope.ActivationDate,
            deactivationdate: $scope.DeActivationDate
        }];

        $http.post('/TS/API/Insert', Service).then(function (res) {
            debugger;
        })

MVC控制器和类:

[HttpPost] 
    public string Insert(ServicesMaster Service)
    {

        GIBCADBEntities gfientity = new GIBCADBEntities();

        var record = "Sent"
        return Json(record, JsonRequestBehavior.AllowGet); 
    } public class ServicesMaster
{
    public string id { set; get; }
    public string servicename { set; get; }
    public string servicetype { set; get; }
    public int? monthlyrental { set; get; }
    public string serviceremarks { set; get; }
    public byte servicestatus { set; get; }
    public DateTime? activationdate { set; get; }
    public DateTime? deactivationdate { set; get; }
}

javascript变量/对象“ EService”在这里还可以,并且在传递时,仅使用空值创建ServicesMaster对象,并且没有数据映射到该对象。 我可以从此处发送单个字符串或任何值,但是在发送完整的对象时,其行为如下。

您正在从前端传递数组,并从服务器端获取对象。 只需在将值设置为EService的情况下删除“ [”和“]”大括号即可。 喜欢 :

 $scope.Get = function () {
     var Service = {};
     Service = {
        id: $scope.Id,
        servicename: $scope.ServiceName,
        servicetype: $scope.ServiceType,
        monthlyrental: $scope.MonthlyRental,
        serviceremarks: $scope.ServiceRemarks,
        servicestatus: $scope.status,
        activationdate: $scope.ActivationDate,
        deactivationdate: $scope.DeActivationDate
     };

     $http.post('/TS/API/Insert', Service).then(function (res) {
        debugger;

     }); 
};

现在应该可以工作了。 :)

暂无
暂无

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

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