Since this is an async method, the return expression must be of type ‘Data’ rather than ‘Task’ Error In ASP dot net entity framework

  Kiến thức lập trình
[HttpGet]
public async Task<IActionResult> View(Guid id)
{
    var employee = await mvcDemoDbContext.Employees.FirstOrDefaultAsync(x => x.Id == id);
    if(employee != null)
    {
        var viewModel = new UpdateEmployeeViewModel()
        {
            Id = employee.Id,
            Name = employee.Name,
            Email = employee.Email,
            Salary = employee.Salary,
            Department = employee.Department,
            DateOfBirth = employee.DateOfBirth

        };
        return View(viewModel);

    }
    return RedirectToAction("Index");
}

In this function I am getting the mentioned error.

How to solve this issue?

when i am using return await View(viewModel); instead of return View(viewModel); then the view isn’t displayed. it’s getting redirect to Index. But When i am trying to use return View(); it shows the view but with not expected results.

Here is the code for View.cshtml file

@model ASPNETMVCCRUD.Models.UpdateEmployeeViewModel

@{
}
<h1> Update Employee Information</h1>
<form method="post" action="View" class="mt-5">
    <div class="mb-3">
        <label for="" class="form-label">Id</label>
        <input type="text" class="form-control" asp-for="Id" readonly>
    </div>
    <div class="mb-3">
        <label for="" class="form-label">Name</label>
        <input type="text" class="form-control" asp-for="Name">
    </div>
    <div class="mb-3">
        <label for="" class="form-label">Email</label>
        <input type="email" class="form-control" asp-for="Email">
    </div>
    <div class="mb-3">
        <label for="" class="form-label">Salary</label>
        <input type="number" class="form-control" asp-for="Salary">
    </div>
    <div class="mb-3">
        <label for="" class="form-label">Date of Birth</label>
        <input type="date" class="form-control" asp-for="DateOfBirth">
    </div>
    <div class="mb-3">
        <label for="" class="form-label">Department</label>
        <input type="text" class="form-control" asp-for="Department">
    </div>

    <button type="submit" class="btn btn-primary">Submit</button>
</form>

LEAVE A COMMENT