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
Sk Jha
NA
113
43.4k
unable to send array data using ajax with asp.net core
Apr 10 2020 12:44 AM
I am trying to send array to the controller but it's blank in the controller parameter.
ajax function is
$(
'#pending'
).click(
function
() {
SaveTestResult(
"/Reception/PatientTests/SavePendingTest"
);
});
function
SaveTestResult(url) {
var
pid = $(
'.patientId'
).attr(
'id'
);
var
tid =
""
;
var
tval =
""
;
var
tpid =
""
;
var
tests = [];
$(
"table > tbody > tr"
).each(
function
() {
testId = $(
this
).find(
'.tid'
).val();
if
(
typeof
(testId) !=
"undefined"
) {
tid = testId;
}
var
rowText =
""
$(
this
).find(
'td'
).each(
function
() {
tpid = $(
this
).find(
'.tpId'
).val();
tval = $(
this
).find(
'.result'
).val();
if
(
typeof
(tpid) !=
"undefined"
) {
tests.push({ PatientId: pid, TestId: tid, TestParameterId: tpid, TestValue: tval });
}
});
});
//alert(JSON.stringify(tests));
$.ajax({
type:
"POST"
,
url: url,
data: JSON.stringify(tests),
contentType:
"application/json"
,
headers: {
"RequestVerificationToken"
: $(
'input[name="__RequestVerificationToken"]'
).val() },
success:
function
(data) {
alert(data);
},
error:
function
(e) {
alert(
'Error'
+ JSON.stringify(e));
}
});
}
this is the controller
[HttpPost]
[Route(
"Reception/PatientTests/SavePendingTest"
)]
public
async Task<IActionResult> SavePendingTest(List<PendingTestResult> pendingTestResult)
{
if
(ModelState.IsValid)
{
foreach
(PendingTestResult ptr
in
pendingTestResult)
{
_db.Add(ptr);
await _db.SaveChangesAsync();
}
// return new JsonResult("Index");
}
return
new
JsonResult(pendingTestResult); ;
}
But when run the code I see data array filled but inside of the SavePendingTest action, pendingTestResult is empty and not filled! I also try [FromBody] tag inside action params but it does not work too! help me to resolve this
Reply
Answers (
2
)
ASP.NET MVC with web api and sql server
unique value store in database using store procedture in ASP