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
- saveStudent(Student){
-
- let headers = new Headers();
- headers.append('Content-Type', 'application/json');
- headers.append('Access-Control-Allow-Origin', '*');
- headers.append('Access-Control-Allow-Methods', 'GET, POST, PUT');
- return this.http.post(environment.apiUrl + 'Student', Student, { headers: headers })
- .pipe(map(data => data.json()),
- catchError((error: any) => {
- throw error;
- }));
- }
Above code get redirected to StudentController which is inherited from ApiController.
There is post method which has return type of void as follows :-
- public void Post([FromBody]Student value)
- {
- _studentRepository.Post(value);
- }
There is interface with name IStudentRepository which contains post as follows : -
- void Post(Student value);
There is StudentRepository which contains following :-
- public void Post(Student objstudent)
- {
- string message = string.Empty;
- string query = "select count(*) from mststundent where StudentName = " + objclass.StudentName;
- MySqlConnection conn = new MySqlConnection(ConnStr);
- conn.Open();
- MySqlCommand objcmd = new MySqlCommand(query);
- int result = Convert.ToInt32(objcmd.ExecuteScalar());
- if (result > 0)
- {
- message = "Exist";
- }
- else
- {
- message = "Inserted";
- }
- conn.Close();
- }
what I want to do is return those messages.
Please let me anyone have easy solution.
Thanking You in adnavce.