Click here to Skip to main content
11,715,425 members (81,204 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVC
I Want to keep search in application can any one help me Here is my code

Controller:
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 :
@{
    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 22-Apr-13 19:04pm
Edited 22-Apr-13 21:31pm
(no name)140K
v6
Comments
Sergey Alexandrovich Kryukov at 23-Apr-13 1:08am
   
Sorry, this is not a question.
—SA
Tadit Dash at 23-Apr-13 3:32am
   
What exactly is the problem ?

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 485
1 Richard MacCutchan 275
2 Maciej Los 259
3 ManasKumarM 155
4 Frankie-C 125
0 Sergey Alexandrovich Kryukov 785
1 CHill60 415
2 Richard MacCutchan 405
3 Maciej Los 389
4 OriginalGriff 320


Advertise | Privacy | Mobile
Web03 | 2.8.150901.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100