[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

search button click show table

Hi Expert,

code is attached.

i want that when my form is loaded then only search form visible to user(means only ui show to user),' and when user click on search button then the grid table display the result.

in this code on form load its showing the records in the grid. please suggest how we can
 do it

////code
@model IEnumerable<MVCDemo.Models.Employee>

@{
    ViewBag.Title = "Index";
}
<div style="font-family:Arial">
<h2>Employee List</h2>
<p>
    @using (@Html.BeginForm("Index", "Home", FormMethod.Get))
    {
        <b>Search By:</b>
        @Html.RadioButton("searchBy", "Name", true) <text>Name</text>
        @Html.RadioButton("searchBy", "Gender") <text>Gender</text><br />
        @Html.TextBox("search") <input type="submit" value="search" />
    }
</p>
<table border="1">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Gender)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email)
        </th>
        <th>Action</th>
    </tr>
@if (Model.Count() == 0)
{
    <tr>
        <td colspan="4">
            No records match search criteria
        </td>
    </tr>
}
else
{
    foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Gender)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
                @Html.ActionLink("Details", "Details", new { id = item.ID }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.ID })
            </td>
        </tr>
    }
}
</table>
</div> 

Open in new window

///

controller

///
public ActionResult Index(string searchBy, string search)
{
    if (searchBy == "Gender")
    {
        return View(db.Employees.Where(x => x.Gender == search || search == null).ToList());
    }
    else
    {
        return View(db.Employees.Where(x => x.Name.StartsWith(search) || search == null).ToList());
    }
}

Open in new window


///////////
table script
Create table tblEmployee
(
 ID int identity primary key,
 Name nvarchar(50),
 Gender nvarchar(10),
 Email nvarchar(50)
)

Insert into tblEmployee values('Sara Nani', 'Female', 'Sara.Nani@test.com')
Insert into tblEmployee values('James Histo', 'Male', 'James.Histo@test.com')
Insert into tblEmployee values('Mary Jane', 'Female', 'Mary.Jane@test.com')
Insert into tblEmployee values('Paul Sensit', 'Male', 'Paul.Sensit@test.com')

Open in new window

0
nicemanish
Asked:
nicemanish
  • 6
  • 3
2 Solutions
 
nicemanishAuthor Commented:
Admin plz look and provide help
0
 
Bob LearnedCommented:
Your question doesn't quite make sense.  You are showing code, but it doesn't help explain what you are asking about.
0
 
nicemanishAuthor Commented:
Its Straight forward.

From my code when page is loded then its showing me the result in table(grid) without any search.which i dont want.

i want when page load only UI display and when user put any criteria in text box and click search button that case its show result on table(grid).

hope its clear
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
Bob LearnedCommented:
Is this the code for the view?

public ActionResult Index(string searchBy, string search)
{
    if (searchBy == "Gender")
    {
        return View(db.Employees.Where(x => x.Gender == search || search == null).ToList());
    }
    else
    {
        return View(db.Employees.Where(x => x.Name.StartsWith(search) || search == null).ToList());
    }
}

Open in new window


It looks like you need an additional variable to indicate when a search is performed, and then render the view including the grid.  You are only passing in the Employees list into the view.  I would think that you need to create a super class that has the state variable and the employees list.

public class EmployeeModel
{
     public List<Employee> EmployeeList { get; set; }
     public bool IsSearch { get; set; }
}

Open in new window

0
 
nicemanishAuthor Commented:
Its look like that I have to find answer from myself.once u will get it post to people how to do it.
0
 
Bob LearnedCommented:
"Its look like that I have to find answer from myself"

Open in new window

What does that mean?  Does that mean that you think that my answer was not good enough, not detailed enough.  I am perfect happy if you do find your own answer.  I feel that it is my task to help you find your own answer.
0
 
nicemanishAuthor Commented:
Thanks bob
I respect you,you have taken my comment otherwise,sorry if I hurt you,
My intension was only that I did not get perfect solution. Its

 look like that I have create other view with post option or use partial view.plz don't mine .if I will get perfect solution I will post to u.  
Thanks a lot for ur cooperation
0
 
nicemanishAuthor Commented:
craete a second action result with parameter ,and take the action as post.
0
 
nicemanishAuthor Commented:
done
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now