Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I Want to keep search in application can any one help me Here is my code

Controller:
C++
public ActionResult Search()
       {
           List<book> bk = new List<book>();
           con.Open();
           string query = "select * from Book where StatusTypeId=1";
           SqlCommand cmd = new SqlCommand(query, con);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           cmd.CommandType = CommandType.Text;
           DataSet ds = new DataSet();
           da.Fill(ds);
           foreach (DataRow dr in ds.Tables[0].Rows)
           {
               bk.Add(new Book()
               {
                   BookId = Convert.ToInt32(dr[0].ToString()),
                   Name = dr[1].ToString(),
                   Author = dr[2].ToString()

               });
           }
           cmd.ExecuteNonQuery();
           return View(bk);
       }

       [HttpPost]
       public ActionResult Search(string Name, Book bk)
       {
           con.Open();
           var genlist = new List<book>();
           string query = "select Name from Book where StatusTypeId=1 and Name=" + Name;
           BookDB db = new BookDB();
           ViewBag.Name = new SelectList(genlist);
           SqlCommand cmd = new SqlCommand(query, con);
           var name = from n in db.bookDb
                      select n;
           if (!string.IsNullOrEmpty(Name))
           {
               name = name.Where(n => n.Name.Contains(Name));

           }
           cmd.CommandType = CommandType.Text;
           cmd.ExecuteNonQuery();
           return RedirectToAction("Index");
       }

View :
Razor
@{
    ViewBag.Title = "Serach";
}
@model IEnumerable<demo.mvc.models.book>
<h2>
</h2>
@*<form action ="@Url.Action()" method="post">
</form>*@
@Html.ValidationSummary(true)
@Html.BeginForm()
<p>
   Name:@Html.TextBox("Name")
    Author:@Html.TextBox("Index")
    input type="button" value="Search" 

<fieldset>
    <table>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <td>
                @Html.DisplayNameFor(model => model.Author)
            </td>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Author)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit1", new { id = item.BookId }) |
                    @Html.ActionLink("View", "View", new { id = item.BookId }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.BookId })
                </td>
            </tr>
        }
    </table>
</fieldset>
Posted
v6
Comments
Sergey Alexandrovich Kryukov 23-Apr-13 1:08am    
Sorry, this is not a question.
—SA
What exactly is the problem ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900