locked
filtering data with Dropdown menu and check boxes so confusing..! RRS feed

  • Question

  • User-64994166 posted

    Is anyone could help me with this code, Actually i am filtering data with Dropdown menu. but i am getting error with this Please see the below code :

    @{
    
        var db = Database.Open("pros");
        var selectedCat =  "";
        var queryProducts = "SELECT ProId, ProTitle " +
            "FROM Products WHERE CategoryId = @0";
        var queryCat = "SELECT DISTINCT CategoryId,CategoryName FROM Categories";
    
        if (IsPost)
                {
                    selectedCat = Request.Form["countrySelection"];
                     
                }
                var d = db.Query(queryProducts,selectedCat);
         var grid1 = new WebGrid(d);
    }
    
    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title></title>
           
        </head>
        <body><form >
            <select id="countrySelection" name="countrySelection">
                    <option>Select a country</option>
                    @foreach (var item in db.Query(queryCat))
                    {
                       if (item.CategoryName == selectedCat)
                    {
                        <option value="@item.CategoryId" selected="true">@item.CategoryName</option>
                    }
                    else
                    {
                        <option value="@item.CategoryId" >@item.CategoryName</option>
                    }
                    }
                </select> 
            </form>
            <div>
            @grid1.GetHtml()
            </div>
            </body>
    </html>
    

    Error is :

    0

    0 : - Input string was not in a correct format.

    Description:

    An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.FormatException: 0 : - Input string was not in a correct format.

    Source Error: 

    Line 13:                  
    Line 14:             }
    Line 15:             var d = db.Query(queryProducts,selectedCat);
    Line 16:      var grid1 = new WebGrid(d);
    Line 17: }


     

    Tuesday, August 23, 2016 11:07 AM

Answers

  • User325035487 posted

    abrsaifi

    if (IsPost) { selectedCat = Request.Form["countrySelection"]; } var d = db.Query(queryProducts,selectedCat); var grid1 = new WebGrid(d); }

    try this instead

        if (IsPost)
                {
                    selectedCat = Request.Form["countrySelection"];
                  var d = db.Query(queryProducts,selectedCat); 
    var grid1 = new WebGrid(d)
    grid1.GetHtml();
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 24, 2016 5:23 AM
  • User-2057865890 posted

    Hi abrsaifi,

    Try this

    @{
        var db = Database.Open("pros");
        var selectedCat = "";
        var queryProducts = "SELECT ProId, ProTitle FROM Products";
        var queryCat = "SELECT DISTINCT CategoryId,CategoryName FROM Categories";
        var d = db.Query(queryProducts);
        if (IsPost)
        {
            selectedCat = Request.Form["countrySelection"];
            queryProducts = "SELECT ProId, ProTitle " +
            "FROM Products WHERE CategoryId = @0";
            d = db.Query(queryProducts, selectedCat);
        }
        var grid1 = new WebGrid(d);
    }

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 24, 2016 8:09 AM
  • User-2057865890 posted

    Hi Abrsaifi,

    i need to filter data using DropDown menu(<select><option></option></select>) and Check boxes without any submit button and you know  i'm really really tired and confused that how to do this. so my request is that please tell me if there is  method which meet to my question.

    You could use js or jQuery.

    Dropdownlist onchange event submit form.

    js

    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "TheForm" })){
    
    <select id="countrySelection" name="countrySelection" onchange="document.getElementById('form1').submit();">
       ...
       ...
    </select> 
    } 
    

    jQuery

    <script type="text/javascript">
      $(function() {
        $("#countrySelection").change(function() {
          $('#TheForm').submit();
        });
      });
    </script>

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 28, 2016 6:40 AM

All replies

  • User325035487 posted

    abrsaifi

    if (IsPost) { selectedCat = Request.Form["countrySelection"]; } var d = db.Query(queryProducts,selectedCat); var grid1 = new WebGrid(d); }

    try this instead

        if (IsPost)
                {
                    selectedCat = Request.Form["countrySelection"];
                  var d = db.Query(queryProducts,selectedCat); 
    var grid1 = new WebGrid(d)
    grid1.GetHtml();
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 24, 2016 5:23 AM
  • User-2057865890 posted

    Hi abrsaifi,

    Try this

    @{
        var db = Database.Open("pros");
        var selectedCat = "";
        var queryProducts = "SELECT ProId, ProTitle FROM Products";
        var queryCat = "SELECT DISTINCT CategoryId,CategoryName FROM Categories";
        var d = db.Query(queryProducts);
        if (IsPost)
        {
            selectedCat = Request.Form["countrySelection"];
            queryProducts = "SELECT ProId, ProTitle " +
            "FROM Products WHERE CategoryId = @0";
            d = db.Query(queryProducts, selectedCat);
        }
        var grid1 = new WebGrid(d);
    }

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 24, 2016 8:09 AM
  • User-64994166 posted

    Hi Chris Zhao: thanks for the response. 

    I did it according too you  Data is displaying  but filtration isn't working as it should be. 

    Wednesday, August 24, 2016 10:34 AM
  • User379720387 posted

    Here is a nice tutorial:

    http://www.mikesdotnetting.com/article/180/displaying-search-results-in-a-webgrid

    Wednesday, August 24, 2016 10:47 AM
  • User-64994166 posted

    Hey Wavemaster :

    you answer is good to learn but  irrelevant to my question actually i need to filter data using DropDown menu(<select><option></option></select>) and Check boxes without any submit button and you know  i'm really really tired and confused that how to do this. so my request is that please tell me if there is  method which meet to my question.

    Thanks & Regards.

    Wednesday, August 24, 2016 11:52 AM
  • User-2057865890 posted

    Hi Abrsaifi,

    i need to filter data using DropDown menu(<select><option></option></select>) and Check boxes without any submit button and you know  i'm really really tired and confused that how to do this. so my request is that please tell me if there is  method which meet to my question.

    You could use js or jQuery.

    Dropdownlist onchange event submit form.

    js

    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "TheForm" })){
    
    <select id="countrySelection" name="countrySelection" onchange="document.getElementById('form1').submit();">
       ...
       ...
    </select> 
    } 
    

    jQuery

    <script type="text/javascript">
      $(function() {
        $("#countrySelection").change(function() {
          $('#TheForm').submit();
        });
      });
    </script>

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 28, 2016 6:40 AM