Pratik Gajjar
Pratik Gajjar

Reputation: 597

How to pass value from MVC view to controller

//Here is my View
 @using 
 (Html.BeginForm("SearchUser", "User", FormMethod.Get))
  {
      <input type="text" id="keyText">
      <input type="submit" value="Search" class="btn btn-success btn-lg" />
  }

In my controller I am having a method for SearchUser -

[HttpGet]
public async Task<IActionResult> SearchUser ([FromQuery] string keyText)
{
    SearchUsersCommandAsync command = new SearchUsersCommandAsync
    {
        Key = keyText
    };

    var response = await new SearchUsersHandler(_db).Handle(command);
    return View("Detail", response);
}

However when i receive the request KeyText always null. Whats wrong I am doing here?

Upvotes: 0

Views: 79

Answers (1)

Shakir Ahamed
Shakir Ahamed

Reputation: 1308

Try this

<input type="text" id="keyText" name="KeyText">

add the name attribute to your input with same name as what you have use parameter in your controller method

Upvotes: 1

Related Questions