Zidane
Zidane

Reputation: 1904

Failing to get textbox value in controller when trying to implement search in ASP.NET Core MVC

I am trying to implement a search so I have a simple form with a textbox. I want to pass the value of the textbox to the controller so that I can begin the logic for my search but unfortunately when my put a breakpoint on my controller I see that the value of the string from the textbox is null and I am not sure why.

My Form:

  <form method="get" asp-action="Search" >
                <div class="input-group">
                    <input id="searchTerm" class="form-control" type ="text" @*asp-for="SearchTerm*@>
                    <div class="input-group-append">
                        <button class="btn btn-primary" type="submit">
                            Search
                        </button>
                    </div>
                </div>
            </form>

My Controller:

public IActionResult Search(IFormCollection Form)
{
    ViewBag.Name = Form["searchTerm"]; 
    return View();
}

Upvotes: 0

Views: 356

Answers (1)

ChinnarajS
ChinnarajS

Reputation: 654

Change form method to post and add a name property to the input tag

<input type="text" id="searchTerm" name="searchTerm" class="form-control" >

Upvotes: 1

Related Questions