locked
how to search in db with 2 dropdown ? RRS feed

  • Question

  • User1928394251 posted

    hello . i have 2 drop dropdown list . when i select first ,second changes. it is right . but i dont know how search in tb from 2 drop down . i dont know what write in controller .

    my view:

    <head>
    <script>

    function Submit()
    {
    $.ajax({
    type:"POST",
    dataType:"json",
    url:'@Url.Action("Savevalue", "entities")',
    data: $('#form5').serialize(),
    success: function (data) {
    }
    });
    window.location.href = "@Url.Action("search11", "entities")";
    };
    </script>
    </head>

    @{ Html.BeginForm("search11", "entities", FormMethod.Get); }

    @Html.DropDownList("txtName5", new List<SelectListItem>
    {
    new SelectListItem { Text = "v1", Value = "v1" },
    new SelectListItem { Text = "v2", Value = "v2"},
    },
    new { @onchange = "CallChangefunc(this.value)" })


    @Html.DropDownList("txtName11", new List<SelectListItem>{
    new SelectListItem { Text = "v3", Value = "v3" },
    new SelectListItem { Text = "v4", Value = "v4" },

    },

    htmlAttributes: new { @id = "vahed1", @name = "vahed1", @style = "display:none" })

    @Html.DropDownList("txtName11", new List<SelectListItem>
    {
    new SelectListItem { Text = "v5", Value = "v5" },
    new SelectListItem { Text = "v6", Value = "v6" },

    },
    htmlAttributes: new { @id = "vahed2", @name = "vahed2", @style = "display:none" })

    <input class="label-warning" type="submit" value="search">

    @{ Html.EndForm(); }

    </div>
    </div>

    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script>
    $(document).ready(function () {
    if ($('#hozeh option:selected').text() == "fani") {
    $("#vahed1").show();
    }
    else if ($('#hozeh option:selected').text() == "sima") {
    $("#vahed2").show();
    }

    });

    function CallChangefunc(val) {
    if (val == "fani") {
    $("#vahed1").show();
    $("#vahed2").hide();

    }

    else if (val == "sima") {
    $("#vahed1").hide();
    $("#vahed2").show();

    }

    in my controller :

    public void Savevalue(string txtName5, string txtName11)
    {

    TempData["hozeh"] = txtName5;
    TempData["vahed"] = txtName11;
    }

    public ActionResult search11()
    {

    string searchString1 = (string)(TempData["hozeh"]);
    string searchString2 = (string)(TempData["vahed"]);


    var entity = from m in db.Notes select m;

    entity = entity.Where(m => m.hozeh == searchString1 && m.vahed == searchString2 &&);

    return View(entity);
    }

    my view shows empty !!!!!!!!!!!!!!!!!!



    Sunday, August 4, 2019 5:35 AM

All replies

  • User1520731567 posted

    Hi aabedeni056,

    I test you code,it only display a dropdownlist and button,like the picture:

    Actually,I can't quite understand what your mean.

    aabedeni056

    i have 2 drop dropdown list . when i select first ,second changes. it is right . but i dont know how search in tb from 2 drop down . i dont know what write in controller .

    Do you mean you have completed the cascade of drop-down boxes and you want to only show 2 drop down whose data from db?

    According to your code,I find some javascript code are not work:

    $(document).ready(function () {
    if ($('#hozeh option:selected').text() == "fani") {//there is no html tag with id being hozeh 
    $("#vahed1").show();
    }
    else if ($('#hozeh option:selected').text() == "sima") {
    $("#vahed2").show();
    }
    
    });
    
    function CallChangefunc(val) {
    if (val == "fani") {  //May be fani should be v1
    $("#vahed1").show();
    $("#vahed2").hide();
    
    }
    
    else if (val == "sima") {//May be fani should be v2
    $("#vahed1").hide();
    $("#vahed2").show();
    
    }

    In addition, your form is empty, so you can't submit any data to db.

    If you have any questions,please post more details and explanation.

    And I recommend you could set aside time to go through the Getting Started tutorials to learn the basics:

    https://www.aspsnippets.com/Articles/ASPNet-MVC-Form-Submit-Post-example.aspx

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/

    Best Regards.

    Yuki Tao

    Monday, August 5, 2019 9:34 AM
  • User1928394251 posted

    hello . thank for your answer .

    i found it . i just condithion for filter (search ) in copndition .

    thank you

    Thursday, August 8, 2019 8:02 AM