0

I have a list of teams on my index page.

I'm trying to pass the text of an input(type text) from the index view back to the index controller, to reload the index page, this time only displaying items in my list which have matching text. eg - bob = bob

Index Controller

  public ActionResult Index(string searchString)        
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";


        var listOfTeams = from T in db.Teams
                     select T; 

        if (!String.IsNullOrEmpty(searchString))
        {
            listOfTeams = listOfTeams.Where(T => T.TeamName.Contains(searchString));

        }


        return View(listOfTeams.ToList());

    }

How i'm trying to pass the data in the Index view

I've tried

 <input type="text" id="inputTeamSearch" name="searchString" class="form-control" style="width:225px;height:60px" onblur="IsTextEmpty()" oninput="CheckTeams()" placeholder="Search">
@Html.ActionLink("Search", "Index")

and

@using(Html.BeginForm("Index", "Team"))
 {

<input type="text" id="inputTeamSearch" name="searchString" class="form-control" style="width:225px;height:60px" onblur="IsTextEmpty()" oninput="CheckTeams()" placeholder="Search">
<input type="submit" id="Index" value="Index" />    

Html.EndForm(); 
 } 

I'm sure this is probably a duplicate of some sort, if so please just pass me in the appropriate direction. I've looked for answers, but they're either long-winded or go into more complex detail than this.

4
  • What is not working for you? What does this do? Commented Jul 4, 2015 at 20:57
  • Apologies for leaving that out. It'll reload the index page, but still display all my teams, ie - the filtering doesn't work. I'm assuming the 'if' statement in the controller is never fired Commented Jul 4, 2015 at 21:33
  • 1
    Check out George Hamwell's answer. He has exactly what you're looking for. Commented Jul 4, 2015 at 22:12
  • In the first snippet, @Html.ActionLink("Search", "Index") wont work - your not passing the value of the search text to the controller. In the second, you need to specify FormMethod.Get and remove Html.EndForm(); (the @using statement adds the closing </form> tag). And what are you javascript functions onblur="IsTextEmpty()" and oninput="CheckTeams()"? Commented Jul 5, 2015 at 1:49

1 Answer 1

2

So to post data to a controller you need a seperate post action which is decorated with the HttpPost attribute. This method needs to take a model as it's parameter:

[HttpPost]
Public ActionResult Index(IndexVM      model)
{
    var searchTerm = model.SearchTerm;
}

The view model needs to contain the fields that you intend to post.

Public class IndexVM
{
     Public String SearchTerm { get; set; }
    //Other model examples
    public Int32 PageNumber { get; set; }
    public Int32 NumOfItemsPerPage { get; set; }
}

Then your html needs to contain a text box that has the same name as the string property in your view model.

@Html.TextBoxFor(m => m.SearchTerm)
//And at the top of your html page you will need to include the model
@model Domain.Models.IndexVM

OR

<input type="text" name="SearchTerm">

Should work.

If you are already using an entity model you can create a new View model which contains the old entity and whatever else you need. So:

public class IndexVM
{
    public Team Team { get; set; }
    public String SearchTerm { get; set; } 
}

Then on your index GET method where you're passing your team to your view you would have:

var view = new IndexVM();
view.Team = //your team object
return View(view);
Sign up to request clarification or add additional context in comments.

2 Comments

When i include "@model Domain.Models.IndexVM" at the top, I get an error saying I can only declare one Model to use, and I need to keep my "@model Domain.Models.Team" or else I can't display info such as the team name.
I got it working in the end, in a slightly different way. Thanks for taking the time to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.