Hi Team,
I am working on web application .Net core 3.1 .
after ajax call . model returned is always null .
Please help to get issue resolved. I have tried all options including adding [FromBody] in controller.
and in startup adding "services.AddRazorPages().AddNewtonsoftJson();"
But still not able to resolve issue.
Below is code snippet.
Controller :
- public async Task<IActionResult> CreateUser([FromBody] UserViewModel viewmodel)
- {
- try
- {
- ...
- }
Startup.cs - Adding services.AddRazorPages().AddNewtonsoftJson();
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddControllersWithViews();
-
- services.AddControllers();
-
-
-
-
- services.AddRazorPages().AddNewtonsoftJson();
- }
ajax call
- var viewModel = {
- UserName: $('#UserName').val(),
- Password: $('#Password').val(),
- FirstName : $('#FirstName').val(),
- SurName: $('#SurName').val(),
- PhoneNumber: $('#PhoneNuber').val(),
- VirtualNumber: $('#VirtualNumber').val(),
- IsActive: true,
- EmailAddress : $('#EmailAddress').val()
- }
-
-
-
-
-
-
-
- $.ajax({
- type: "POST",
- contentType: "application/json",
- data: JSON.stringify(viewModel),
- url: "/Users/CreateUser",
- success: function (data) {
-
- },
- error: function (error, statusText) {
-
- }
- });
Model
- public class UserViewModel
- {
- public Guid UserID { get; set; }
-
-
-
- public int ID { get; set; }
- public string UserName { get; set; }
- public string Password { get; set; }
- public string FirstName { get; set; }
- public string SurName { get; set; }
- public string PhoneNumber { get; set; }
- public string VirtualNumber { get; set; }
- public bool IsActive { get; set; }
- public bool EmailAddress { get; set; }
- }
Please help me out in identifying what am i missing.
Regards,
Kiran