0

I am trying to display list of items from database based on dropdownlist selection which is hard coded in ASP.NET MVC application.

My Controller

 public ActionResult ListofItems()
            {
                ListofClassClassHandle listofClassClassHandle = new ListofClassClassHandle ();
                return View(listofClassClassHandle.LeadingAll());
            }

ListofClassClassHandle Class

public List<Leading> LeadingAll()
        {
            clsUtilities clsUtilities = new clsUtilities();
            DataSet ds;
            List<Leading> leading = new List<Leading>();
            string sSQL;
            sSQL = "exec GetLeading 'NZ'";
            ds = clsUtilities.GetDataSet(sSQL);
            DataTable dataTable = ds.Tables[0];
            foreach(DataRow dr in dataTable.Rows)
            {
                leading.Add(
                    new Leading
                    {
                        RankId = Convert.ToInt32(dr["RankId"]),
                        Name = Convert.ToString(dr["Name"]),

                    }               
                    ); 
            }

Leading Class

public class Leading
    {
        public int RankId { get; set; }
        public string Name{ get; set; }   
        public Countries AllCountries { get; set; }

    }
public enum Countries
    {
        New_Zealand,
        Australia      
    }

Leading View

 @Html.DropDownList("AllCountries",
        new SelectList(Enum.GetValues(typeof(Countries))),
        "Select Country",
         new { @class = "form-control", style = "width: 150px;" })

<table class="table">
    <tr>
        <th>
            @Html.DisplayName("Rank")
        </th>
        <th>
            @Html.DisplayName("Name")
        </th>


    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.RankId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>

        </tr>
    }

</table>

I want to display the list based on dropdown country list selection. The dropdown list is hard coded where the list of data is populating from database.

Please guide me. I do not have any idea how to accomplish this? Any help or guidance is highly appreciated.

Raj
  • 555
  • 1
  • 12
  • 31
  • 2
    What isn't working? Displaying data from a database is generally covered by any tutorial. – David Dec 05 '17 at 22:31
  • You need to pass the selected option value of dropdown to your GET action method as a parameter and pass that to your data access method and use that when calling the stored proc. Have you tried that ? You can keep your dropdown inside a form tag with a submit button , which can submit the form the same GET action method. – Shyju Dec 05 '17 at 22:34
  • @Shyju, I have not tried this. But the dropdown list is using enum to get the countries. So how can i get the value from this one? Can you please guide me. – Raj Dec 05 '17 at 22:45
  • Ultimately it simply render a SELECT element with options. So you read the selected value and pass it to your data access method. – Shyju Dec 05 '17 at 22:46
  • Posted an answer explaining how to pass the selected value to the action method code and from there to your data access method. – Shyju Dec 05 '17 at 22:49

1 Answers1

1

You can pass the dropdown selection to your GET action method and from there to your data access method and use that to get the filtered set of data.

To start, add a parameter to your action method.

public ActionResult ListofItems(string AllCountries="")
{
    var h = new ListofClassClassHandle();
    return View(h.LeadingAll(AllCountries));
}

And update the LeadingAll method to accept this parameter. Update your data access code to use the value inside the country variable.

public List<Leading> LeadingAll(string country)
{
    // to do : use the value of country to call the stored procedure
    // to do : return list of Leading objects
}

Now in your View, you can keep your dropdown list inside a form tag along with a submit button. Set the form tag's action attribute value to your ListOfItems action method and set the form method attribute to GET.

@using (Html.BeginForm("ListOfItems", "Home", FormMethod.Get))
{
    @Html.DropDownList("AllCountries",
            new SelectList(Enum.GetValues(typeof(Countries))),
            "Select Country",
            new { @class = "form-control", style = "width: 150px;" })
    <input type="submit" value="Filter" />
}

When the user selects an option from the SELECT element and click submit button it will make a GET call with the selection in the querystring ( Ex :/ListOfItems/?AllCountries=Australia) and your action method code will use this value to get the data for that country by passing that to your data access method.

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Thank you so much for your guidance. I will try this and will see how it works. If i stuck anywhere then I will let you know. Thank you once again. – Raj Dec 05 '17 at 22:54
  • Shyju, Sorry to interrupt you. Can you please guide me how to get value from Enum. As from database, the value is coming like "NZ" for New Zealand, "AUS" for Australia. As you can see this code sSQL = "exec GetLeading 'NZ'". Please guide me. Thank you. – Raj Dec 06 '17 at 00:13
  • No. You cannot do that with enum (straight out of the box). There are some workarounds where you can use the Description attribute and use LINQ to get the description and value and use that to build the dropdown. Your other option is to fix the data in the table or the enums so that the data match. Or keep some sort mapping ( a dictionary) which maps NewZeland to NZ and use that to get the code before calling the stored proc. Or Use a Country table and store the CountryId in your other table. – Shyju Dec 06 '17 at 00:36
  • See this https://stackoverflow.com/questions/1415140/can-my-enums-have-friendly-names – Shyju Dec 06 '17 at 00:37
  • Shyju, I removed Enum from the Leading class and added all the countries in View. @using (Html.BeginForm("ListOfItems", "Home", FormMethod.Get)) { } Please guide me now how to do this. Thank you. – Raj Dec 06 '17 at 00:57
  • How to do what ? If your options have the same value as your data in the table, you are good. When you submit the form, the query string will have that (option value, `NZ` or `AZ`) and you need to update your other code to use that value to get the filtered data as i mentioned in the answer.' – Shyju Dec 06 '17 at 01:02
  • Shyju, Can you please guide me how to grab option value from View and use that value into this class. Please guide me. I am stuck here. public List LeadingAll(string country) { } – Raj Dec 06 '17 at 01:37
  • That is exactly what i have in the answer. You have to do a form submit and you will have the selected option value in your action method parameter. Read the answer carefully – Shyju Dec 06 '17 at 01:38
  • Shyju, I did what you said to me. But string AllCountries=" " is blank. This is the code @using (Html.BeginForm("ListOfItems", "Home", FormMethod.Get)) { } . Whenever i select country, it is passing null there. – Raj Dec 06 '17 at 01:43
  • Your select element should have a name attribute with value `AllCountries` `` – Shyju Dec 06 '17 at 01:44
  • Shyju, Thank you so much. It worked for me now. – Raj Dec 06 '17 at 01:47