Click here to Skip to main content
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 20:04pm
Edited 22-Apr-13 22:31pm
(no name)117.5K
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 OriginalGriff 275
1 DamithSL 265
2 CPallini 235
3 Maciej Los 185
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2014
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