As part of learning i was working on creating a angular form and connecting it to a web api,when i post data from form,it is getting as null values in the api, I have tried adding a [Route("jsonbody")] but that is not working, also tried JSON.stringify(model) in the service but that is also not working.
This is how i posted the data from angular
onSubmit(form: NgForm) {
this.registerobj.postUserData(this.model).subscribe()
}
Model class is
export class UserModel {
constructor(
public FirstName: string ='',
public LastName: string = '',
public Address: string = '',
public DateOfBirth: string = '',
public Gender: string = '',
public Language: string = '',
public Email: string = ''
) {}
}
Posted the data from service like this
postUserData(model: UserModel) {
return this.http.post<any>(`${this.serviceUrl}`, model)
}
.Net Core api Controller
[Route("api/[controller]")]
public class UserController : Controller
{
public UserController(IUser userItems)
{
UserItems = userItems;
}
public IUser UserItems { get; set; }
[HttpPost]
public IActionResult Insert(UserModel user)
{
UserRepository UserItems = new UserRepository();
try
{
if (user == null)
{
return BadRequest("Recieved a Bad request");
}
UserItems.Add(user);
return Ok();
}
catch(Exception e)
{
return StatusCode(500, e);
}
}
}
UserModel in API
public class UserModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string DateOfBirth { get; set; }
public string Gender { get; set; }
public string Language { get; set; }
public string Email { get; set; }
}