0

The parameter, search, comes from Index view by Ajax post. After the search process I want to send the employees object to Result view. Result action and view are in the same controller and same view folder. But RedirectToAction(employees) doesn't affect. There is no problem about getting search value from view by Ajax or about getting corresponding employees from database, all of them are fine. This post says you cannot redirect from Ajax post. I don't know how can I redirect & send the employees object to Result view. I don't want to make this via ViewBag.

[HttpPost]
public async Task<IActionResult> Result([FromBody]string search)
{
    if (string.IsNullOrEmpty(search))
    {
        return NotFound();
    }

    IEnumerable<Employee> employees = await _context.Employees.Where(e => e.Surname == search).ToListAsync();
    return RedirectToAction("Index", employees);
}

$(document).ready(function () {
    $('.SearchButton').on('click', function (e) {
        e.preventDefault();
        var searchVal = $('.Search').val();
        console.log(searchVal);
        $.ajax(
        {
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            url: '@Url.Action("Index", "Employee")',
            data: JSON.stringify(searchVal),
            dataType: 'json',
            success: function (response) {                
                console.log(response);
            },
            error: function (response) {
                console.log(response.message);
            }
        });
    });
});
Orhun
  • 1,162
  • 14
  • 22
  • 2
    So that post also says, that you can redirect from javascript by assigning redirect URL value to `window.location.href` (you can do this in the `success` callback). – Set Jul 06 '17 at 07:31
  • @Set but how can I send the employees with redirection? – Orhun Jul 06 '17 at 07:45
  • ok, my bad, this may help only if you can use query parameters. For POST data, you may construct and fill out a hidden `method=POST action="http://redirect_url" form` and submit. Look into [Send POST data on redirect with Javascript/jQuery](https://stackoverflow.com/q/19064352/2833802) – Set Jul 06 '17 at 07:59
  • I want to handle this by .NET MVC way. Any other way do you know? – Orhun Jul 06 '17 at 08:11
  • @JamesP Yes, you're right I can do it by TempData, ViewBag etc. but I want to handle this more professional way(if there is a way like that). – Orhun Jul 06 '17 at 11:21
  • @Orhun Since you are redirecting, what is the point of doing it in Ajax or normal POST method? – Anuraj Jul 07 '17 at 04:14

1 Answers1

0

I created a Result view instead of trying to redirect to Index view, removed the HttpPost and FromBody attribute from Result action, changed the "return RedirectToAction("Index", employees)" to "return View(employee)"

public async Task<IActionResult> Result(string search)
{
    if (string.IsNullOrEmpty(search))
    {
        return NotFound();
    }

    IEnumerable<Employee> employees = await _context.Employees.Where(e => e.Surname == search).ToListAsync();
    return View(employees);
}

<input type="text" class="Search" placeholder="Search by surname" /><a class="SearchButton">Search</a>

Then delete the whole jQuery code and replace with this lines of code:

$(document).ready(function () { 
    $('.SearchButton').on('click', function (e) {
        window.location.href = '@Url.Action("Result")' + '/?search=' + $('.Search').val();
    });
});
Orhun
  • 1,162
  • 14
  • 22