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?