TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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
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.
Reply
Answers (
1
)
API Data Consuming to Data Table In client side Using Ajax
What is Restfull service? Can any one give simple example