简体   繁体   中英

ASP.NET MVC Filter data according to the options selected in the drop-down list

I'm new to this whole MVC world, please excuse me if the problem is simple.

I'm trying to is build a project that can let the user should be able to select the field to search in (LastName, FirstName, Department, and Location) using a drop-down list. And the user can type the search term in a text box and clicks the Search button. (Filter data according to the options selected in the drop-down list, and then enter the content that matches the options selected in the drop-down list)

The problem is: I can only search in a field, and the value in my drop-down list cannot be read by the text box.

My model is this:

public class Employee
    {
        public int EmployeeId { get; set; }

        [Required]
        [StringLength(50, MinimumLength = 1)]
        public string LastName { get; set; }

        [Required]
        [StringLength(50)]
        public string FirstName { get; set; }

        [Required]
        public string Department { get; set; }

        [StringLength(50)]
        public string Location { get; set; }
    }

My Controller is this:

public ActionResult Index(string sortOrder, string searchString)
        {
            ViewBag.EmployeeList = new List<SelectListItem>
            {
                new SelectListItem{Selected=true, Text="LastName", Value="LastName" },
                new SelectListItem{Selected=true, Text="FirstName", Value="FirstName" },
                new SelectListItem{Selected=true, Text="Department", Value="Department" },
                new SelectListItem{Selected=true, Text="Location", Value="Location" },
            };

            var employee = from e in db.Employee
                           select e;

            if (!String.IsNullOrEmpty(searchString))
            {
                employee = employee.Where(s => s.FirstName.Contains(searchString));
            }
            return View(employee);
        }

And my View is this:

<p>
        All: @Html.DropDownList( "EmployeeList");
        Search By: @Html.TextBox("searchString")<br />
        <input type="submit" value="Search">
    </p>

I don't know how can I use the drop-down list to select the corresponding field and let the search box search. For example, if I select "Location", I can enter some keywords about "Location" in the text box to search, but it is limited to Location, not FirstName or others

This is the controller where selectOption and searchtext are passed from the View

  public ActionResult Index(string SelectOption, string SearchText)
    {

        var model = from s in db.Employee
                    select s;

        if (!String.IsNullOrEmpty(SearchText) )
        {
            switch (SelectOption)
            {
                case "Email":
                    model = model.Where(a => 
a.Email.ToLower().Contains(SearchText.ToLower()));
                    break;

                case "Forname":
                    model = model.Where(a => 
a.Forename.ToLower().Contains(SearchText.ToLower()));
                    break;

                case "Surname":
                    model = model.Where(a => 
a.Surname.ToLower().Contains(SearchText.ToLower()));
                    break;

            }
        }
       



        return View(model.ToList());
    }

As for the view, you do need to use viewbag all the time for dropdownlist. Sometimes pure plain html can do a better job.

@model IEnumerable<Incendo.Entities.Employee>

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm())
{

<label for="selectoption">Select Option:</label>
<select id="SelectOption" name="SelectOption">
    <option value="Email">Email</option>
    <option value="Surname">Surname</option>
    <option value="Forename">Forename</option>
</select>

<br />

@Html.Label("Search Text")
@Html.TextBox("SearchText") 
<input type="submit" value="Search" />
}


<table class="table table-responsive">
@foreach (var item in Model)
{
    <tr>
        <td>@item.Forename </td>
        <td>@item.Surname </td>
        <td>@item.Email  </td>
    </tr>
}

Finally, please remember to change the model reference to your own namespace

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM