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
599
1.9k
144.3k
asp.net core razor pages - cascade dropdownlists
Dec 19 2020 12:06 AM
I tried to implement the example here
https://www.c-sharpcorner.com/article/creating-simple-cascading-dropdownlist-in-asp-net-core-mvc-with-new-tag-helpers/
into my Razor page project but the second dropdown doesn't get any data. What I did is
View Page including script
<div
class
=
"col-2"
>
<label
for
=
"PTWReviewD.Category"
class
=
"form-control badge-primary"
>PTW Review Category</label>
<select asp-
for
=
"PTWReviewD.Category"
class
=
"form-control border-warning"
asp-items=
"Model.PTWRCList"
>
<option value=
""
>--Select PTW Review Category--</option>
</select>
<span asp-validation-
for
=
"PTWReviewD.Category"
class
=
"text-danger"
></span>
</div>
<div
class
=
"col-2"
>
<label
for
=
"PTWReviewD.SubCategory"
class
=
"form-control badge-primary"
>PTW Review Subcategory</label>
<select asp-
for
=
"PTWReviewD.SubCategory"
class
=
"form-control border-warning"
asp-items=
"@(new SelectList(string.Empty, "
RSubCategoryID
", "
RSubCategoryS
"))"
>
<option value=
""
>--Select PTW Review Subcategory--</option>
</select>
<span asp-validation-
for
=
"PTWReviewD.SubCategory"
class
=
"text-danger"
></span>
</div>
@Html.AntiForgeryToken()
@section scripts{
<script>
$(function () {
$(
"#Category"
).on(
"change"
, function func() {
var url =
'@Url.Content("~/")'
+
"PTWReview/OnGetSubCategoriesAsync"
;
var idSource =
"#RCategoryID"
;
$.getJSON(url, { RCategoryID: $(idSource).val() }, function (data) {
var items =
''
;
$(
"#SubCategory"
).empty();
$.each(data, function (i, ptwrsclist) {
items +=
"<option value='"
+ ptwrsclist.RSubCategoryID +
"'>"
+ ptwrsclist.RSubCategoryS +
"</option>"
;
});
$(
"#SubCategory"
).html(items);
});
});
});
</script>
}
Getting the data with
public
async Task<IActionResult> OnGetAsync(
int
ptwNoId)
{
//PTWRCategory
PTWRCList = await _context.PTWRCategories.Select(a =>
new
SelectListItem
{
Value = a.RCategoryID.ToString(),
Text = a.RCategoryS
}).ToListAsync();
return
Page();
}
public
async Task<JsonResult> OnGetSubCategoriesAsync(
int
RCategoryID)
{
PTWRSCList = await _context.PTWRSubCategories.Where(a => a.RCategoryID == RCategoryID).Select(a =>
new
SelectListItem
{
Value = a.RSubCategoryID.ToString(),
Text = a.RSubCategoryS
}).ToListAsync();
return
new
JsonResult(
new
SelectList(PTWRSCList,
"RSubCategoryID"
,
"RSubCategoryS"
));
}
The first ddl is filled with data but the second is not and I have no errors. What am I doing wrong?
Reply
Answers (
3
)
Page transfer to "ccav.aspx" that time I got error
Why I cann't update one column by details View Control.