2015-09-28 77 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的數組格式。我在編輯答案,請檢查並讓我知道。 –