Home > Enterprise >  i cant populate select in jquery,my code is in razor c#, i try this
i cant populate select in jquery,my code is in razor c#, i try this

Time:12-05

the content of the table is this enter image description here

@section scripts{
        <script>
            function adddata() {

                $.ajax({
                    type: "POST",
                    url: '?handler=GetItems',
                    headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
                    data: { Id: $("#IdSelectIdPuesto").val() },
                    success: function (data) {
                        for (var i = 0; i < data.length; i  ) {
                        $("#IdSelectlocalidad").append("<option value='"   data[i].value  
                        "' selected>"   data[i].text   "</option>");
                        

                        }
                    },
                    error: function (result) {
                        alert("fail");
                    }
                })
            }
        </script>
    }


public JsonResult OnPostGetItems(int Id)
        {
            //displaydata1 =  rTDBContext.Turnos.ToList();
            
            var displayda = (from c in displaydata1
                             select c.LocNombre).Distinct().ToList();
            
            return new JsonResult(new List<SelectListItem> = { new SelectListItem { Value = "1", Text = "LOcalidad"   1 }, new SelectListItem { Value = "2", Text = "LOcalidad"   2 } });
            
        }

i'm trying a select distinct and add to the specified select the id and the locNombre to the text

CodePudding user response:

If you want to change the options of $("#IdSelectlocalidad") with the id passed from ajax,here is a demo:

public JsonResult OnPostGetItems(int Id)
        {
            //displaydata1 =  rTDBContext.Turnos.ToList();
            var displayda = (from c in l
                             where c.Id==Id select new SelectListItem { Text=c.LocNombre, Value=c.Id ""}).Distinct().ToList();
           
            return new JsonResult(displayda);

        }

js(remove selected in the option,with the code it will select the first option by default):

@section scripts{
        <script>
            function adddata() {

                $.ajax({
                    type: "POST",
                    url: '?handler=GetItems',
                    headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
                    data: { Id: $("#IdSelectIdPuesto").val() },
                    success: function (data) {
                        for (var i = 0; i < data.length; i  ) {
                        $("#IdSelectlocalidad").append("<option value='"   data[i].value  
                        "' >"   data[i].text   "</option>");
                        

                        }
                    },
                    error: function (result) {
                        alert("fail");
                    }
                })
            }
        </script>
    }

CodePudding user response:

[thanks Yiyi You, ifound the correct answer i doit this way]

[enter image description here][1]

[1]: https://i.stack.imgur.com/KWsIi.png do as folow

displaydata1 =  rTDBContext.Turnos.ToList();
        var display = displaydata1.DistinctBy(x => x.LocNombre ).ToList();
        var displayda = (from c in display
                         select new SelectListItem { Text = c.LocNombre, Value = c.IdLocalidad   "" }).Distinct().ToList();

now is working as desired.

  • Related