StillLearning
StillLearning

Reputation: 111

Search String not retaining in the search box

The filters are working fine and I am getting the right data , but I am unable to retain the Search String in the search box. I am not able to figure out what's wrong in the code

[HttpGet]
public async Task<IActionResult> GetAllLeaves(int? page, string sortOrder, string currentFilter, string employeeName, string employeeId = null, )
{
    var leaves = await _employeeLeaveRepository.GetAllLeavesOfUsers();

    if (!String.IsNullOrEmpty(employeeName))
    {
        leaves = leaves.Where(e => e.FullName.ToLower().Contains(employeeName.ToLower()));
        // return View (employeeViewList.ToList());
    }
    else
    {
        employeeName = currentFilter;
    }

    ViewBag.CurrentFilter = employeeName;

    // .............
    // other code
    // .............

    int pageSize = 10;
    int pageNumber = (page ?? 1);

    return View(leaves.OrderByDescending(s => s.LeaveStatus).ToPagedList(pageNumber, pageSize));
}

The view:

<form method="get" class="mt-5" asp-controller="Leave" asp-action="GetAllLeaves">
    <div class="form group col-md-6">
        <label>Employee </label>
        <div class="col">
            <input type="hidden" id="employeeId" name="employeeId" />
            <input type="text" name="employeeName"  id="employeeName" />
        </div>
    </div>
</form>

Upvotes: 0

Views: 173

Answers (1)

mj1313
mj1313

Reputation: 8469

You can store the employeeName in the ViewBag:

ViewBag.EmployeeName= employeeName;

Then set it as the value of the input:

<input type="text" name="employeeName"  id="employeeName" value="@ViewBag.EmployeeName" />

Upvotes: 1

Related Questions