最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net core webapi - I can't do nested filtering on the page using .Net blazor - Stack Overflow

programmeradmin4浏览0评论

I am doing a full stack project using .NET Core and Blazor. I am trying to run Blazor in the web part but I am stuck at one point. When adding an exam, I want the exams to be listed in the box next to the selected course and I want the question in the selected exam to be recorded there.

My area of interest in Blazor page is as follows

<EditForm Model="_quiz" OnValidSubmit="SaveQuizAsync">
<DataAnnotationsValidator />
<div class="row">
    <div class="col-sm-3">
        <div class="mb-3">
            <label class="form-label">Dersler</label>
            <InputSelect @bind-Value="_quiz.LessonId" class="form-control" @onchange="LoadExamsAsync">
                <option value="0">Ders Seç</option>
                @foreach (var c in _lessonDtos)
                {
                    <option value="@c.Id">@c.Name</option>
                }
            </InputSelect>
            <ValidationMessage For="@(() => _quiz.LessonId)" />
        </div>
    </div>
    <div class="col-sm-3">
        <div class="mb-3">
            <label class="form-label">Exams</label>
            <InputSelect @bind-Value="_quiz.ExamId" class="form-control">
                @if (_examDtos.Any())
                {
                    <option value="0">Exam Seç</option>
                    @foreach (var e in _examDtos)
                    {
                        <option value="@e.Id">@e.Name</option>
                    }
                }
                else
                {
                    <option value="0">Bu derse ait sınav bulunamadı</option>
                }
            </InputSelect>
            <ValidationMessage For="@(() => _quiz.ExamId)" />
        </div>

My SaveQuizAsync method looks like this:

private async Task SaveQuizAsync()
{
    var errorMessage = _quiz.TryValidate();

    if (!string.IsNullOrWhiteSpace(errorMessage))
    {
        await ShowAlertAsync(errorMessage);
        return;
    }

    var response = await QuizApi.CreateQuizzesAsync(_quiz);

    if (!response.IsSuccess)
    {
        await ShowAlertAsync(response.ErrorMessage);
        return;
    }

    NavigationManager.NavigateTo("/admin/manage-quizzes");
}

The method I created to list the relevant exams is as follows

private async Task LoadExamsAsync(ChangeEventArgs e)
{
    Console.WriteLine("LoadExamsAsync triggered"); // Bu satır tetikleniyor mu kontrol edin

    if (Guid.TryParse(e.Value?.ToString(), out var lessonId))
    {
        Console.WriteLine($"Selected LessonId: {lessonId}");
        _examDtos = (await ExamApi.GetExamsByLessonId(lessonId))?.ToArray() ?? Array.Empty<ExamDto>();
        StateHasChanged(); // Sayfayı güncellemek için
    }
    else
    {
        Console.WriteLine("Invalid LessonId");
    }
}

On the backend side, I share my related codes as ordinary as below

The field where I call the related method on the API side

[HttpGet("{lessonId:guid}")]
public async Task<IActionResult> GetExamsByLessonId(Guid lessonId, bool trackChanges)
{
    try
    {
        // İlgili derse ait sınavları alıyoruz
        var exams = await _manager.ExamService.GetExamsByLessonIdAsync(lessonId, trackChanges);

        // Eğer sınav bulunamadıysa, uygun bir mesaj döndürüyoruz
        if (!exams.Any())
        {
            return NotFound($"No exams found for the lesson with ID {lessonId}");
        }

        // Sınavları başarıyla bulduysak, onları döndürüyoruz
        return Ok(exams);
    }
    catch (Exception ex)
    {
        // Hata durumunda uygun mesaj dönüyoruz
        return BadRequest($"An error occurred while fetching exams: {ex.Message}");
    }
}

The relevant method in the service layer of the call I use in the controller is as follows

 public async Task<IEnumerable<ExamDto>> GetExamsByLessonIdAsync(Guid lessonId, bool trackChanges)
{
    // Repository'den sınavları alıyoruz
    var exams = await _manager.Exam.GetExamByLessonIdAsync(lessonId, trackChanges);

    // Eğer sınavlar yoksa, boş bir liste döndürüyoruz
    if (!exams.Any())
    {
        return new List<ExamDto>();
    }

    // Sınavları DTO'ya dönüştürüyoruz
    var examDtos = exams.Select(e => new ExamDto
    {
        Id = e.Id,
        Name = e.Name,
        LessonId = e.LessonId, // LessonId'yi alıyoruz
        LessonName = e.Lesson.Name
       
    });

    return examDtos;
}

What I want to do is to list the exams of that course in the exams box on the side when the course is selected and add the question to that exams

I tried to write all the things that came to my mind, it was a little long, but I tried to make it understandable.

I am doing a full stack project using .NET Core and Blazor. I am trying to run Blazor in the web part but I am stuck at one point. When adding an exam, I want the exams to be listed in the box next to the selected course and I want the question in the selected exam to be recorded there.

My area of interest in Blazor page is as follows

<EditForm Model="_quiz" OnValidSubmit="SaveQuizAsync">
<DataAnnotationsValidator />
<div class="row">
    <div class="col-sm-3">
        <div class="mb-3">
            <label class="form-label">Dersler</label>
            <InputSelect @bind-Value="_quiz.LessonId" class="form-control" @onchange="LoadExamsAsync">
                <option value="0">Ders Seç</option>
                @foreach (var c in _lessonDtos)
                {
                    <option value="@c.Id">@c.Name</option>
                }
            </InputSelect>
            <ValidationMessage For="@(() => _quiz.LessonId)" />
        </div>
    </div>
    <div class="col-sm-3">
        <div class="mb-3">
            <label class="form-label">Exams</label>
            <InputSelect @bind-Value="_quiz.ExamId" class="form-control">
                @if (_examDtos.Any())
                {
                    <option value="0">Exam Seç</option>
                    @foreach (var e in _examDtos)
                    {
                        <option value="@e.Id">@e.Name</option>
                    }
                }
                else
                {
                    <option value="0">Bu derse ait sınav bulunamadı</option>
                }
            </InputSelect>
            <ValidationMessage For="@(() => _quiz.ExamId)" />
        </div>

My SaveQuizAsync method looks like this:

private async Task SaveQuizAsync()
{
    var errorMessage = _quiz.TryValidate();

    if (!string.IsNullOrWhiteSpace(errorMessage))
    {
        await ShowAlertAsync(errorMessage);
        return;
    }

    var response = await QuizApi.CreateQuizzesAsync(_quiz);

    if (!response.IsSuccess)
    {
        await ShowAlertAsync(response.ErrorMessage);
        return;
    }

    NavigationManager.NavigateTo("/admin/manage-quizzes");
}

The method I created to list the relevant exams is as follows

private async Task LoadExamsAsync(ChangeEventArgs e)
{
    Console.WriteLine("LoadExamsAsync triggered"); // Bu satır tetikleniyor mu kontrol edin

    if (Guid.TryParse(e.Value?.ToString(), out var lessonId))
    {
        Console.WriteLine($"Selected LessonId: {lessonId}");
        _examDtos = (await ExamApi.GetExamsByLessonId(lessonId))?.ToArray() ?? Array.Empty<ExamDto>();
        StateHasChanged(); // Sayfayı güncellemek için
    }
    else
    {
        Console.WriteLine("Invalid LessonId");
    }
}

On the backend side, I share my related codes as ordinary as below

The field where I call the related method on the API side

[HttpGet("{lessonId:guid}")]
public async Task<IActionResult> GetExamsByLessonId(Guid lessonId, bool trackChanges)
{
    try
    {
        // İlgili derse ait sınavları alıyoruz
        var exams = await _manager.ExamService.GetExamsByLessonIdAsync(lessonId, trackChanges);

        // Eğer sınav bulunamadıysa, uygun bir mesaj döndürüyoruz
        if (!exams.Any())
        {
            return NotFound($"No exams found for the lesson with ID {lessonId}");
        }

        // Sınavları başarıyla bulduysak, onları döndürüyoruz
        return Ok(exams);
    }
    catch (Exception ex)
    {
        // Hata durumunda uygun mesaj dönüyoruz
        return BadRequest($"An error occurred while fetching exams: {ex.Message}");
    }
}

The relevant method in the service layer of the call I use in the controller is as follows

 public async Task<IEnumerable<ExamDto>> GetExamsByLessonIdAsync(Guid lessonId, bool trackChanges)
{
    // Repository'den sınavları alıyoruz
    var exams = await _manager.Exam.GetExamByLessonIdAsync(lessonId, trackChanges);

    // Eğer sınavlar yoksa, boş bir liste döndürüyoruz
    if (!exams.Any())
    {
        return new List<ExamDto>();
    }

    // Sınavları DTO'ya dönüştürüyoruz
    var examDtos = exams.Select(e => new ExamDto
    {
        Id = e.Id,
        Name = e.Name,
        LessonId = e.LessonId, // LessonId'yi alıyoruz
        LessonName = e.Lesson.Name
       
    });

    return examDtos;
}

What I want to do is to list the exams of that course in the exams box on the side when the course is selected and add the question to that exams

I tried to write all the things that came to my mind, it was a little long, but I tried to make it understandable.

Share Improve this question edited Jan 19 at 17:53 marc_s 754k184 gold badges1.4k silver badges1.5k bronze badges asked Jan 19 at 17:09 MehmetMehmet 516 bronze badges 8
  • 1 What is the problem? You've told us what you want to do, and posted your code, but haven't explained where you're having a problem with it. – Jason Commented Jan 19 at 20:41
  • @Jason I actually explained, when the course is selected in the section that says courses in the last image, the Exams section should be filtered and listed according to the selected course, but it says exam not found as in the image – Mehmet Commented Jan 19 at 21:19
  • have you stepped through the code in GetExamsByLessonIdAsync to narrow down the source of the problem? – Jason Commented Jan 19 at 22:22
  • @Jason There is no problem on the API side, I can access the data but I cannot trigger GetExamsByLessonIdAsync on the Blazor side – Mehmet Commented Jan 20 at 9:04
  • 1 Assuming LoadExamsAsync isn't called, the problem may be due to an incorrect interactivity/rendering mode. What's the interactivity mode of this component? And how is the project structured? If you want your code to run on the browser, you need a separate Client project that uses either InteractiveAuto or InteractiveWebAssembly. That's explained in the Client-Side Rendering section of the Render Modes docs – Panagiotis Kanavos Commented Jan 23 at 8:06
 |  Show 3 more comments

1 Answer 1

Reset to default 1

If you use Apple System, there's a networking feature called App Transport Security (ATS) which may block connections that fail to meet minimum security specifications.

As a workaround, you may add the NSAppTransportSecurity key to your app’s Info.plist and providing an ATS configuration dictionary as the value. You may try adding the following key&value to your Info.plist,

<dict>    
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>

For more info, please refer to NSAppTransportSecurity.

发布评论

评论列表(0)

  1. 暂无评论