【问题标题】:Pass an Object from Angularjs to MVC controller and map to Class Object将对象从 Angularjs 传递到 MVC 控制器并映射到类对象
【发布时间】:2017-01-11 08:57:58
【问题描述】:

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

 $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 对象,并且没有数据映射到它。我可以从这里发送单个字符串或任何值,但是当发送一个完整的对象时,它的行为是这样的。

【问题讨论】:

    标签: javascript c# angularjs asp.net-mvc


    【解决方案1】:

    您正在从前端传递一个数组并从服务器端获取对象。只需在将值设置为 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;
    
         }); 
    };
    

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

    【讨论】:

      猜你喜欢
      • 2013-08-17
      • 2015-09-13
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 2014-01-25
      • 2011-05-14
      • 1970-01-01
      相关资源
      最近更新 更多