I know this question has been asked multiple times in Stakoverflow.But none of the solution is helping me .Can anyone tell me what had been done wrong
I have a Controller Action as below
<HttpPost()>
Function WFUserEdited(ByVal userdetails As userdetails) As ActionResult
Return View()
End Function
And My Viewmodel is like below
Public Class userdetails
Public Property Username As String
Public Property Password As String
Public Property Fullname As String
Public Property Location As String
Public Property Appcode As String
Public Property Userlevel As String
End Class
And My Jquery Ajax call is like below
$("#btnSubmit").click(function () {
var q = validate();
if (q = true) {
var userdetails = {};
userdetails.UserName = $('#txtusername').val().toString();
userdetails.Password = $('#txtPassword').val().toString();
userdetails.Fullname = $('#txtfullName').val().toString();
userdetails.Location = $('#txt_location').val().toString();
userdetails.Appcode = $("#drp_appcode").chosen().val().toString();
userdetails.Userlevel = $("#drp_userlevel").chosen().val().toString();
userdetails = JSON.stringify(userdetails);
var url = '@Url.Action("WFUserEdited", "WFUser")';
$.ajax({
url: url,
type: 'POST',
traditional: false,
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: { "userdetails": userdetails },
success: function (data) {
},
error: function (data) {
}
});
}
});
});
And at The Beginexecutecore iam getting the below exception
Protected Overrides Function BeginExecuteCore(callback As AsyncCallback, state As Object) As IAsyncResult
Try
ViewBag.UserImage = "http://_layouts/15/CaptarisWFTasksWebPart/EmployeeImages/sree.jpg"
Catch generatedExceptionName As Exception
Return MyBase.BeginExecuteCore(callback, state)
End Try
Return MyBase.BeginExecuteCore(callback, state)
End Function
An exception of type 'System.ArgumentException' occurred in System.Web.Mvc.dll but was not handled in user code Additional information: Invalid JSON primitive: userdetails.
userdetails
so its justdata: userdetails
(or easier - just delete theuserdetails = JSON.stringify(userdetails);
line and thecontentType: "application/json; charset=utf-8",
option and just usedata: userdetails
(and yourtraditional: false,
is pointless, as are all your.toString()
)