0

I am trying to implement a search functionality on my index page, but i can't get my head around how it should be.

I am simply trying to do a simple search, so I put a number in the input box, and on button click i display under the form the object that matcher that id.

So far I have a controller method, which migh be wrong, and a view(which also mugh be wrong).

Thee view looks like this (the search part in the bottom) :

<h2>Search</h2>
@model IEnumerable<class_project.Models.Post>

@{
    var i = 1;
    foreach (var item in Model)
    {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Content)
        </td>
        <td>

            @Html.ActionLink("Details", "Details", new { id = i }) 
           @{
               i++;
        }
        </td>
    </tr>
        }
    }

@using (Html.BeginForm("Search", "Posts", FormMethod.Post))
{   <p>
        @Html.TextBoxFor()
    </p>

    <input type="submit" />

@Html.Action("Search", "Post")

Controller method :

 public ActionResult Search(int id)
        {
            return View(db.Posts.Find(id));
        }

Model :

public class Post
    {

    public int PostId { get; set; }

    public string Title { get; set; }

    public string Content { get; set; }


    public Post(int PostId, string Title, string Content)
    {
        this.PostId = PostId;
        this.Title = Title;
        this.Content = Content;
    }
    public Post()
    {
    }

I am banging my head with this simple task, so please, can someone help me with this?

Rohan Khude
  • 4,455
  • 5
  • 49
  • 47
Robert Ross
  • 1,151
  • 2
  • 19
  • 47

1 Answers1

1

You have to use input using name attribute to reference form data after your form is submitted to server-side.

@using (Html.BeginForm("Search", "Posts", FormMethod.Post))
{   
   <p>
      <input type="number" name="id"/>
   </p>
   <input type="submit" />
}
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128