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
kiran a
NA
20
9.3k
Model is not getting bind from Ajax Call - .Net core 3.1
Dec 22 2020 7:19 PM
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.AddMvc();
services.AddControllers();
//.AddNewtonsoftJson(options =>
//{
// options.SerializerSettings.ContractResolver = new DefaultContractResolver();
//});
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()
}
//url = "/Users/CreateUser";
//$.post(url, { UserViewModel: JSON.stringify(viewModel) })
// .done(function (response, status, jqxhr) {
// alert("ass");
// })
// .fail(function (jqxhr, status, error) {
// })
$.ajax({
type:
"POST"
,
contentType:
"application/json"
,
data: JSON.stringify(viewModel),
url:
"/Users/CreateUser"
,
success:
function
(data) {
//return callback(data);
},
error:
function
(error, statusText) {
// return callback(error);
}
});
Model
public
class
UserViewModel
{
public
Guid UserID {
get
;
set
; }
/// <summary>
/// Db Id - Unique. To be used by for all calls.
/// </summary>
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
Reply
Answers (
3
)
How do I work with multiple Comboxes in an ASP .NET Core MVC Form?
asp.net core Change span color if @Html.DisplayFor has no data