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
Marius Vasile
552
1.9k
145.8k
asp.net core getting data from json and jquerry
Apr 14 2021 3:45 PM
I have two select and I want to populate the second but I don't know what am I doing wrong
<div
class
=
"col-md-10 mb-2"
>
<select id=
"IRSelect"
class
=
"form-control ml-1"
>
<option value=
""
>--Select Filtering Condition--</option>
<option value=
"1"
>Issuer</option>
<option value=
"2"
>Receiver</option>
</select>
</div>
<div
class
=
"col-md-10"
>
<select id=
"IRDetail"
class
=
"form-control ml-1"
asp-items=
"@(new SelectList(string.Empty, "
Issuer
", "
Issuer
"))"
>
<option value=
""
>--Select Name--</option>
</select>
</div>
and json
public
async Task OnGetIRDetails1Async()
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var orgid = await _context.UsersData.Where(s => s.Id == userId).Select(s => s.OrgID).FirstOrDefaultAsync();
var todayYear = DateTime.Today.Year;
var valIR = await _context.PTWContents.Where(s => s.OrgID == orgid && s.StartDate.Year == todayYear).Select(s =>
new
SelectListItem
{
Value = s.Issuer,
Text = s.Issuer
}).ToListAsync();
return
new
JsonResult(
new
SelectList(valIR ,
"Issuer"
,
"Issuer"
));
}
public
async Task OnGetIRDetails2Async()
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var orgid = await _context.UsersData.Where(s => s.Id == userId).Select(s => s.OrgID).FirstOrDefaultAsync();
var todayYear = DateTime.Today.Year;
var valIR = await _context.PTWContents.Where(s => s.OrgID == orgid && s.StartDate.Year == todayYear).Select(s =>
new
SelectListItem
{
Value = s.Receiver,
Text = s.Receiver
}).ToListAsync();
return
new
JsonResult(
new
SelectList(valIR ,
"Receiver"
,
"Receiver"
));
}
and jquery
$(
function
() {
$(
"#IRSelect"
).on(
"change"
,
function
() {
var
categoryId = $(
this
).val();
if
(categoryId = 1) {
$(
"#IRDetail"
).empty();
$(
"#IRDetail"
).append(
"<option value=''>--Select Name--</option>"
);
$.getJSON(`?handler=IRDetails1`, (data) => {
$.each(data,
function
(i, item) {
$(
"#IRDetail"
).append(`<option value=
"${item.value}"
> ${item.text}</option>`);
});
});
}
if
(categoryId = 2) {
$(
"#IRDetail"
).empty();
$(
"#IRDetail"
).append(
"<option value=''>--Select Name--</option>"
);
$.getJSON(`?handler=IRDetails2`, (data) => {
$.each(data,
function
(i, item) {
$(
"#IRDetail"
).append(`<option value=
"${item.value}"
> ${item.text}</option>`);
});
});
}
});
});
Reply
Answers (
3
)
filling dropdownlist doesn't work
Azure function in visual studio and displaying output in MVC using C#