2015-09-28 78 views
-2

我正在处理EAV数据库模式。 我的模型是这样的:我只是想通过列表到asp.net web api使用angularjs

public class LeadsModel 
{ 
    public int? CompId { get; set; } 
    public int LeadID { get; set; } 
    public string LeadName { get; set; } 
    public string source { get; set; } 
    public string status { get; set; } 
    public int UserId { get; set; } 

    [Required] 
    public List<AttributesModel> AList { get; set; } 

} 

我的看法是这样的。鉴于我正在获取属性列表,我想回发使用angularjs。

<div class="form-group" ng-repeat="At in Attributes" > 
         <label for="{{At.Attri}}" class="col-md-4 control-label">{{At.Attri}}</label> 
         <div class="col-md-8"> 
          @*<input type="hidden" name="{{At.AID}}" data-ng-model="newLead.NewAlist" />*@ 
          <input type="text" class="form-control" id="{{At.Attri}}" name="{{At.Attri}}" pl placeholder="Enter {{At.Attri}}" data-ng-model="newLead.AList.AttriValue" ng-blur="AddItemToList(newLead.Alist.AttriValue)" /> 
         </div> 
        </div> 

我的角度代码是这样的

$scope.add = function() 
    { 
     $scope.loading = true; 
     this.newLead.AList = $scope.listt; 
     $http.post('/api/Leads/Posttbl_Lead', this.newLead).success(function (data) { 
      alert("Added Successfully!!"); 
      $scope.loading = false; 
      $scope.addLMode = false; 
     }) 
     .error(function() { 
      $scope.error = "An Error has occured while loading posts!"; 
      $scope.loading = false; 
     }); 
    } 

和我的Web API控制器是这样

public IHttpActionResult Posttbl_Lead(LeadsModel tbl_Lead) 
    { 
     if (!ModelState.IsValid) 
     { 
      return BadRequest(ModelState); 
     } 
     tbl_Lead newLead = new tbl_Lead(); 
     newLead.LeadName = tbl_Lead.LeadName; 
     newLead.source = tbl_Lead.source; 
     newLead.status = tbl_Lead.status; 
     newLead.LeadName = tbl_Lead.LeadName; 
     newLead.CompId = tbl_Lead.CompId; 

     db.tbl_Lead.Add(newLead); 
     db.SaveChanges(); 

     return CreatedAtRoute("DefaultApi", new { id = tbl_Lead.LeadID }, tbl_Lead); 
    } 
+0

我在这里没有看到问题。 –

回答

1

使用此代码后AngularJs您纽利德tbl_Lead你的API控制器。这是您将列表/数组对象传递给You API的补充link

$http({ 

    contentType: "application/json; charset=utf-8",//required 

    method: "POST", 

    url: '/api/Leads/Posttbl_Lead', 

    dataType: "json",//optional 

    data:{ "tbl_Lead": newLead }, 

    async: "isAsync"//optional 

}) 
    .success(function (response) { 

     alert('Saved Successfully.');      

    }) 
    .error(function() { 
     $scope.error = "An Error has occured while loading posts!"; 
     $scope.loading = false; 
    }); 

编辑-1

下文提到的是送ALIST内LeadsModel于API的方式。

LeadsModel通过API发送到服务器上。

{ 
    CompId=compId, 
    LeadID=leadID, 
    AList=[{FirstObject=firstObject},{SecondObject=secondObject}] 
} 
+0

感谢您的回复,我可以如何发送AList。 –

+0

我无法弄清楚究竟是什么让你变得越来越复杂。为了将任何列表发送到API,它应该是JSON的数组格式。我在编辑答案,请检查并让我知道。 –

相关问题