Filter Data using DropDownList in ASP.Net Core MVC

sourav688
 
on Jun 14, 2020 11:38 PM
Sample_490281.zip
7238 Views

First, I am using ASP.NET Core 3.1 MVC.

I have a table which contain Class wise Subject list.

Example- Class 8 (Maximum 5 subject can be taken by student), have subject A, B, C,D, E, F, G, H, I, J

Different School/College provides different subject/subject combination for this class. For Example - In Some School/College, In Class-8 subject D is not available, or some other School/College, In Class-8 subjects E, F are not available.

So, School Have to enter the subject they taught in each class. In Create Action (GET) method "List of Class" is shown in Dropdown list. After selecting a Class (For Example, Class 8) and click on Search option, It will show all available Subject for that particular class, Then School/College select the subjects which is available in that School/College and Save(POST Method) it to database.

How do I implement this Search functionality in Create page action method ?

Graphical representation:-

Select Class -----1,2,3,4,5,6,7,8,9,10,11,12,13 (DropDownList) [Search Button]

Select any class then click search. Then List of Subject Shown below in checkbox

Subject 1[checkbox]

Subject 2[checkbox]

Subject 3[checkbox]

Subject 4[checkbox]

Subject 5[checkbox]

Subject 6[checkbox]

Subject 7[checkbox]

Subject 8[checkbox]

Subject 9[checkbox]

Subject 10[checkbox]

Subject 11[checkbox]

Subject 12[checkbox]


Back To List [Button]                           Save [Button]

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jun 18, 2020 02:17 AM

Hi sourav688,

Refer below sample.

Model

public class SubjectCheckBoxVM
{
    public List<Subjects> Subjects { get; set; }
    public int ClassID { get; set; }
}

public class Subjects
{
    public int SubjectID { get; set; }
    public string Subject { get; set; }
    public bool IsSelected { get; set; }
}

Controller

public class HomeController : Controller
{
    // GET: /Home/
    public IActionResult Index()
    {
        BindClassDropDownList();
        return View(new SubjectCheckBoxVM());
    }

    [HttpPost]
    public IActionResult Search(SubjectCheckBoxVM model)
    {
        TestEntities entity = new TestEntities();
        List<Subjects> result = (from cws in entity.tlb_ClassWiseSubject
                                    join s in entity.tlb_Subject on cws.SubjectID equals s.SubjectID
                                    where cws.ClassID == model.ClassID
                                    select new Subjects
                                    {
                                        SubjectID = s.SubjectID,
                                        Subject = s.Subject
                                    }).ToList();
        model.Subjects = result;
        BindClassDropDownList();
        return View("Index", model);
    }

    private void BindClassDropDownList()
    {
        TestEntities entity = new TestEntities();
        string classType = "Upper Prmary";
        int ClassTypeID = entity.tlb_ClassType.Where(x => x.ClassType == classType).Select(x => x.ClassTypeID).FirstOrDefault();
        SelectList selectList = new SelectList(entity.tlb_Class.Where(x => x.ClassTypeID == ClassTypeID).ToList(), "ClassID", "Class");
        TempData["Class"] = selectList;
    }
}

View

@model Search_DropDownList_Core_MVC.Models.SubjectCheckBoxVM

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form asp-action="Search" asp-controller="Home" method="post">
        Select Class: @Html.DropDownListFor(m => m.ClassID, (IEnumerable<SelectListItem>)TempData["Class"],"Select")
        <input type="submit" value="Search" />
        <hr />
        @if (Model.Subjects != null){
        <table>
            @foreach (var item in Model.Subjects){
            <tr>
                <td>
                    @Html.HiddenFor(x=>item.SubjectID)
                    @Html.CheckBoxFor(x=>item.IsSelected)
                    @item.Subject
                </td>
            </tr>
            }
        </table>
        }   
    </form>
</body>
</html>

Screenshot