Dear all,
I am Begineer in MVC 5 and Angular 6 in which I am developing one web application.
There is one angular component(web page) - User.html and User.ts where I am trying to save data in two tables separately i.e. and .
On angular component(web page) i.e. User.html and User.ts, I have separate button to save User header data in table and there is button which opens popup window to save User details data in table.
I am passing data from Angular component to APIcontroller in MVC 5(UserController.cs).
On UserController.cs, I have two POST methods i.e. one for Header and another for Details
UserController.cs
- using DomainDb.Models;
- using DomainDb.Repository;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Http;
- using System.Web.Http.Cors;
- using System.Web.Mvc;
-
- namespace CURDApis.Controllers
- {
-
- [EnableCors(origins: "*", headers: "*", methods: "*")]
- public class UserController : ApiController
- {
-
- private IUserRepository _userRepository;
-
- public UserController()
- {
-
- _userRepository = UnityFactory.ResolveObject();
- }
-
-
- public List GetRightsList(string srights, string srights1)
- {
- return _userRepository.GetRightsList(srights, srights1);
- }
-
-
- public List Get()
- {
- return _userRepository.Get();
- }
-
-
- public User Get(int id)
- {
- return _userRepository.Get(id);
- }
-
-
-
- public void Delete(int id)
- {
- _userRepository.Delete(id);
- }
-
-
- public void Post([FromBody]User value)
- {
- _userRepository.Post(value);
- }
-
-
- public void post([FromBody]User svalue, string suser)
- {
- _userRepository.Post(svalue, suser);
- }
- }
- }
Code of public void Post([FromBody]User value) written in a REPOSITORY called as UserRepository.cs - code for inserting or updating data in table
-
-
-
- public void Post(User objUser)
- {
- GetAuthentications objGetAuthenication = new GetAuthentications();
- objGetAuthenication.Add = objUser.Add;
- objGetAuthenication.Modify = objUser.Modify;
- objGetAuthenication.Deactivate = objUser.Deactivate;
- objGetAuthenication.View = objUser.View;
- string auth = objGetAuthenication.GetAuthentication(objGetAuthenication);
- string InsertQuery = objUser.UserID == 0 ?
- @"INSERT IGNORE INTO mstuserheader(UserID,UserName, UserPassword,AuthenticationType,UserRightsID,Activate)
- VALUES (?UserID,?UserName, ?UserPassword, ?AuthenticationType,?UserRightsID,?Activate );
- SELECT LAST_INSERT_ID();" :
- @"UPDATE mstuserheader SET UserName = ?UserName,UserPassword = ?UserPassword,AuthenticationType = ?AuthenticationType,UserRightsID = ?UserRightsID,Activate = ?Activate WHERE UserID = " + objUser.UserID;
- using (MySqlConnection connEpion = new MySqlConnection(ConnStr))
- {
- connEpion.Open();
- int ID = objUser.UserID;
- using (MySqlCommand objcmd = new MySqlCommand(InsertQuery))
- {
- objcmd.Connection = connEpion;
- objcmd.Parameters.AddWithValue("UserID", objUser.UserID);
- objcmd.Parameters.AddWithValue("AuthenticationType", auth);
- objcmd.Parameters.AddWithValue("UserRightsID", objUser.UserRightsID);
- objcmd.Parameters.AddWithValue("Activate", objUser.Activate);
- int i = objcmd.ExecuteNonQuery();
- }
- connEpion.Close();
- }
- }
Code of public void post([FromBody]User svalue, string suser) written in a REPOSITORY called as UserRepository.cs - code for inserting or updating data in table
- public void Post(User objUserRights,string suser)
- {
- GetAuthentications objGetAuth = new GetAuthentications();
- objGetAuth.Add = objUserRights.Add;
- objGetAuth.Modify = objUserRights.Modify;
- objGetAuth.Deactivate = objUserRights.Deactivate;
- objGetAuth.View = objUserRights.View;
-
- string auth = objGetAuth.GetAuthentication(objGetAuth);
- string query = string.Empty;
-
- using (MySqlConnection connEpion = new MySqlConnection(ConnStr))
- {
- connEpion.Open();
-
- using (MySqlCommand objcmd = new MySqlCommand("select count(*) from mstuserrightsdetails where UserRightsID = " + objUserRights.UserRightsID + " and ComponentID = " + objUserRights.ComponentID))
- {
- objcmd.Connection = connEpion;
-
- int i = Convert.ToInt32(objcmd.ExecuteScalar());
- if (i == 0)
- {
-
- query = @"INSERT IGNORE INTO mstuserrightsdetails(UserRightsID,ComponentID,AuthenticationType,Activate)
- VALUES (?UserRightsID,?ComponentID, ?AuthenticationType,?Activate );";
- }
- else
- {
-
- query = @"UPDATE mstuserrightsdetails SET AuthenticationType = ?AuthenticationType,
- Activate = ?Activate WHERE UserRightsID = " + objUserRights.UserRightsID + " and ComponentID = " + objUserRights.ComponentID;
- }
- objcmd.CommandText = query;
- objcmd.Parameters.AddWithValue("UserRightsID", objUserRights.UserRightsID);
- objcmd.Parameters.AddWithValue("ComponentID", objUserRights.ComponentID);
- objUserRights.AuthenticationType = auth;
- objcmd.Parameters.AddWithValue("AuthenticationType", objUserRights.AuthenticationType);
- objcmd.Parameters.AddWithValue("Activate", objUserRights.Activate);
- i = objcmd.ExecuteNonQuery();
- }
- connEpion.Close();
- }
- }
When I am trying to call second post method(public void Post(User objUserRights,string suser)) for saving UserDetails data then I am getting default values for User class and it gets stored into table.
here is code for User.cs
here is code for User.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace DomainDb.Models
- {
-
- public class User
- {
- public int UserID { get; set; }
- public int UserDetailsID { get; set; }
public int ComponentID { get; set; }
- public string UserName { get; set; }
- public string UserPassword { get; set; }
- public bool Add { get; set; }
- public bool Modify { get; set; }
- public bool Deactivate { get; set; }
-
- }
- }