-2

我正在研究 EAV 数据库模式。我的模型是这样的:

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>

我的 Angular 代码是这样的

 $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);
    }
4

1 回答 1

1

使用此代码将AngularJs 的newLead发布到API 控制器的tbl_Lead 。这是您将列表/数组对象传递给 You API的补充链接。

$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

下面提到的是将 LeadsModel 中的 AList 发送到您的 api 的方法。

LeadsModel通过 API 发送到服务器。

{
    CompId=compId,
    LeadID=leadID,
    AList=[{FirstObject=firstObject},{SecondObject=secondObject}]
}
于 2015-09-28T05:41:27.960 回答