Mandar Shinde

Mandar Shinde

  • NA
  • 423
  • 114.5k

Returns messages record exist or insert from MVC to Angular

Feb 20 2019 12:39 AM

Dear all,

I am beginner in ASP .net MVC and Angular 5. I was trying to insert record using Angular 5 to MVC using API Controller.
 
Here is my TypeScript file code
  1. saveStudent(Student){  
  2.        
  3.    let headers = new Headers();  
  4.    headers.append('Content-Type''application/json');  
  5.    headers.append('Access-Control-Allow-Origin''*');  
  6.    headers.append('Access-Control-Allow-Methods''GET, POST, PUT');  
  7.    return this.http.post(environment.apiUrl + 'Student', Student, { headers: headers })  
  8.      .pipe(map(data => data.json()),  
  9.      catchError((error: any) => {  
  10.        throw error;  
  11.    }));  
  12.  }  
Above code get redirected to StudentController which is inherited from ApiController.
There is  post method which has return type of void as follows :-
 
  1. public void Post([FromBody]Student value)  
  2. {  
  3.   _studentRepository.Post(value);  
  4. }  
There is interface with name IStudentRepository which contains post as follows : - 
  1. void Post(Student value);  
There is StudentRepository which contains following :-
  1. public void Post(Student objstudent)  
  2.         {  
  3.             string message = string.Empty;  
  4.             string query = "select count(*) from mststundent where StudentName = " + objclass.StudentName;  
  5.             MySqlConnection conn = new MySqlConnection(ConnStr);  
  6.             conn.Open();  
  7.             MySqlCommand objcmd = new MySqlCommand(query);  
  8.             int result = Convert.ToInt32(objcmd.ExecuteScalar());  
  9.             if (result > 0)  
  10.             {  
  11.                 message = "Exist";  
  12.             }  
  13.             else  
  14.             {  
  15.                 message = "Inserted";  
  16.             }  
  17.             conn.Close();  
  18.         }  
what I want to do is return those messages.
Please let me anyone have easy solution.
Thanking You in adnavce. 

Answers (1)