2017-03-08 6 views
0

これは、番号なしリスト内のデータ形式のデータベースとディスプレイを取り、私のHTTP GETリクエストメソッドであるASP.Net WebAPIのポストメソッドを格納し、null値

$("#ulEmployees").ready(function() 
      { 
      var ulEmployees = $('#ulEmployees'); 
      $("#btn").click(function() { 
       $.ajax({ 
        type: 'GET', 
        url: 'api/Employees', 
        dataType: 'json', 
        success: function (data) { 
         ulEmployees.empty(); 
         $.each(data, function (index, val) { 
          var info = val.Employee_Name + ' Works for ' + val.Employee_Department + ' Department.'; 
          ulEmployees.append('<li>' + info + '</li>') 


         }); 
        } 
       }); 
      }); 
     }); 

GETリクエストメソッドが正常に動作しますが、ここで私のPOSTでありますnull値をデータベースにポストするリクエストメソッド。データベースここenter image description here

$(document).ready(function(){ 
       $("#insertbutton").click(function() { 
        //var emp = new Object(); 
        var name = $("#employeename").val(); 
        var dep = $("#insertbutton").val(); 

        $.ajax({ 
         url: 'api/Employees', 
         type: 'POST', 
         dataType: 'json', 
         // data: emp, 
         data:'{"Employee_Name":"' +name+'","Employee_Department": "' +dep+'"}', 
         success: function() { 
          alert("Poduct is Inserted Successfully"); 
         } 
        }); 
       }); 
       }); 

出力はコントローラ内部でPOSTリクエストを処理するための方法であって、ここで

public void Post(Employee emp) 
     { 
      CompanyEntities ent = new CompanyEntities(); 
      ent.Employees.Add(emp); 
      ent.SaveChanges(); 
     } 

は、私は答えを得た

public partial class Employee 
    { 
     public int Employee_Id { get; set; } 
     public string Employee_Name { get; set; } 
     public string Employee_Department { get; set; } 
    } 
} 
+0

。 – DavidG

+0

Employeeクラスの定義も共有できますか? –

答えて

0

従業員クラスの定義です。私は、POSTリクエスト内のいくつかの変更を加えて、それが働いた:

$(document).ready(function(){ 
      $("#insertbutton").click(function() { 
       //var emp = new Object(); 
       var name = $("#employeename").val(); 
       var dep = $("#insertbutton").val(); 
       // console.log(name); 

       var sendinfo = { 
        Employee_Name: name, 
        Employee_Department: dep 

       }; 

       $.ajax({ 
        url: 'api/Employees', 
        type: 'POST', 
        dataType: 'json', 
        // data: emp, 
        data:sendinfo, 
        success: function() { 
         alert("Employee Inserted Successfully"); 
        } 
       }); 
      }); 
      }); 

参考:Send JSON data via POST (ajax) and receive json response from Controller (MVC)あなたはまた、POSTリクエストを受信し、あなたのC#コードを表示する必要が

関連する問題