0

I have two controllers, one List (), which displays all tasks, and ListSelect (), which returns tasks today, tomorrow, etc. When an onChance event occurs, I call the ListSelect () controller and this controller returns the selected data, and I need display them on the page. But I don't know how. thank you in advance.

Controllers

    public ActionResult<IEnumerable<TaskReadDto>> ListSelect(int typeSelect)
    {
        var userId = HttpContext.User.Identity.Name;
        var tasks = taskRepository.GetByDate(typeSelect, Convert.ToInt32(userId));
        var result = mapper.Map<List<TaskReadDto>>(tasks);
        return View("_ViewAll", result);
    }

    [HttpGet]
    public ActionResult<IEnumerable<TaskReadDto>> List()
    {
        var userId = HttpContext.User.Identity.Name;
        var tasks = userRepository.GetWithTasksById(Convert.ToInt32(userId));
        var result = mapper.Map<List<TaskReadDto>>(tasks);
        return View(result);
    }

List.cshtml

@using ToDoList.Domain;
@model IEnumerable<ToDoList.Dto.TaskReadDto>;

<div class="d-flex justify-content-between mt-5">
<div class="d-flex flex-column">
    <div class="m-3">
        <a onclick="showInPopup('@Url.Action("Add","Task",null,Context.Request.Scheme)','Нове завдання')"
           class="btn btn-success text-white">Додати</a>
    </div>
</div>
<div class="m-3">
    <select id="ddlAjax" class="form-control form-control-sm">
        <option value="1">Сьогодні</option>
        <option value="2">На завтра</option>
        <option value="3">На тиждень</option>
    </select>
</div>
</div>
<div id="view-all">
    @await Html.PartialAsync("_ViewAll", Model)
</div>

_ViewAll.cshtml

@model IEnumerable<ToDoList.Dto.TaskReadDto>;
@if (@Model.Any())
{
<div>
<table class="table table-bordered">
    <thead>
        <tr>
            <th scope="col">Назва</th>
            <th scope="col">Термін</th>
            <th scope="col">Важливість</th>
            <th scope="col">Статус</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var task in Model)
        {
<tr>
<th scope="row">@task.Name</th>
<td>
    @task.EnrollDeadline.ToShortDateString()
</td>
<td>
    @Html.DisplayFor(modelItem => task.Importance)
</td>
<td>
    @Html.DisplayFor(modelItem => task.Status)
</td>
<td class="text-right">
    <a class="btn btn-secondary btn-sm text-white" 
onclick="showInPopup('@Url.Action("Details","Task",new 
{id=task.Id},Context.Request.Scheme)','Деталі завдання')">Details</a>
    <a class="btn btn-primary btn-sm text-white" onclick="showInPopup('@Url.Action("Edit","Task",new {id=task.Id},Context.Request.Scheme)','Редагування завдання')">Edit</a>
    <a class="btn btn-danger btn-sm text-white" onclick="showInPopup('@Url.Action("Delete","Task",new 
{id=task.Id},Context.Request.Scheme)','Видалити завдання')">Delete</a>
</td>
</tr>
}
    </tbody>
</table>
</div> }
  else
  {
  <h3>Жодного завдання ще не додано!</h3>}
JHyrdvi
  • 23
  • 5
  • 2
    Hi @JHyrdvi, 1. what is your `onChance` event? Do you mean onchange? 2. For your such backend code, you can use ajax to call the `ListSelect` method and then use `.html` jquery method(e.g. `$("#view-all").html(res)`) in ajax success function to render the data. 3. I also suggest you can combine `List` and `ListSelect` action to one action and then onchange method will call `window.location.href` to call the action with selected value as `typeSelect` parameter. More details you can refer to [this answer](https://stackoverflow.com/a/66593280/11398810). – Rena Sep 17 '21 at 08:06

0 Answers0