Filter Records in ASP.NET MVC

Hursh 191 Reputation points
2024-06-20T05:14:05.8133333+00:00

The following code searches students by students name and it works fine.

I want to add 3 radio button (Enrolled, Not Enrolled, All Students) and filter records based on the radio button selection. How do I do it?

Also, is there a way to put these radio buttons in a group box and some spaces between the buttons?

O Enrolled O Not Enrolled O All Students

<p style="text-align:right; font-weight:bolder; color:blue">
    @Html.ActionLink("New Enrollment", "Create")
</p>

<form method="get" action="@Url.Action("Index")">
    <p>

        Search by Student Name: <input type="text" name="searchString" value="@ViewData["CurrentFilter"]" />        
<input type="submit" value="Search" class="btn btn-default" /> 
        @Html.ActionLink("Display Students", "Index")
    </p>

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,344 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,396 questions
{count} votes

Accepted answer
  1. Tiny Wang-MSFT 2,171 Reputation points Microsoft Vendor
    2024-06-20T08:07:27.59+00:00

    Hi @Hursh, I had a test in my side with an MVC application, here's my codes, could you please help check whether the radio buttons I used solved your issue? In my test, my radio button selected option will be passed to the Controller action. Then we can modify the query clause to query out the required data.

    @model WebAppDefIdentity.Models.MovieGenreViewModel
    @{
        ViewData["Title"] = "Index";
    }
    <h1>Index</h1>
    <form asp-controller="Movies" asp-action="Index" method="get">
        <p>
            Title: <input type="text" asp-for="SearchString" />
           
            Gender:
            <span class="col-md-10">
                <span style="margin-left:10px;">
                    @Html.RadioButtonFor(model => model.Gender, "Male") Male
                </span>
                <span style="margin-left:10px;">
                    @Html.RadioButtonFor(model => model.Gender, "Female") Female
                </span>
            </span>
            <input type="submit" value="Filter" />
        </p>
    </form>
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Movies![0].Title)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Movies![0].ReleaseDate)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Movies![0].Genre)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Movies![0].Price)
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.Movies!)
            {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Title)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ReleaseDate)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Genre)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Price)
                </td>
            </tr>
    }
        </tbody>
    </table>
    public class MovieGenreViewModel
    {
        public List<Movie>? Movies { get; set; }
        public string? SearchString { get; set; }
        public string? Gender { get; set; }
    }
    public async Task<IActionResult> Index(string searchString, string gender)
    {
        var movies = from m in _context.Movie
                     select m;
        if (!string.IsNullOrEmpty(searchString))
        {
            movies = movies.Where(s => s.Title!.Contains(searchString));
        }
        var movieGenreVM = new MovieGenreViewModel
        {
            Movies = await movies.ToListAsync()
        };
        return View(movieGenreVM);
    }
    
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Tiny

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful