Home > OS >  Fetch the data from the drop down list in the view
Fetch the data from the drop down list in the view

Time:10-04

I want to fetch the data from the drop down list in the view,and the data is already in my mssql database.

There is a column named "Address",and I used the column value to filter the data in database to get what I want,and this is my code.

[HttpPost]
public JsonResult GetPharmacy(string cityName, string areaName)
{
Project1Entities db = new Project1Entities();
var query = (from a in db.Pharmacies
where a.Adress == cityName
select a);
var query1 = (from b in query
where b.Adress == areaName
select b);
if (string.IsNullOrEmpty(areaName))
{
return Json(new EmptyResult());
}
return Json(query1);
}

The "cityname" and "areaname" has value, but when I send the request to the database with the button in my view,it returns nothing...

Here is my function in ajex:

function SerchallData(selectedCity) {
            var selectedCity = $('#CITY option:selected').val();
            var selectedValue = $('#AREA option:selected').val();
            if ($.trim(selectedValue).length > 0) {
                $.ajax({
                    url: '@Url.Action("GetMaskmap", "Getarea")',
                    data: { cityName: selectedCity, areaName: selectedValue },
                    type: 'POST',
                    dataType: 'json',
                    success: function (data) {
                        $('#Table>tbody').empty();
                        for (var i = 0; i < data.length; i  ) {
                            var row = $('<tr><td>'   data[i].ID   '</td><td>'   data[i].Name   '</td><td>'   data[i].Adress   '</td><td>'    data[i].Phonenumber  '</td></tr>');
                            $('#Table>tbody').append(row);
                            
                        }
                        
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert('Error: '   textStatus   ' - '   errorThrown);
                    }
                });

I wonder where is the problem that I could not get the value return from the database?

CodePudding user response:

Your query doesn't make sense. In the first part you request records that have an Adress equal to cityName. In the second part you filter that down to records, that have an Adress equal to areaName. If cityName and areaName are not equal, the result will always be empty.

As the sequential combination of two Where queries is equal to a single query with both conditions combined with a logical "AND", you can rewrite your request like this, which makes the mistake obvious:

var query = (from a in db.Pharmacies
   where a.Adress == cityName && a.Adress == areaName
   select a);

CodePudding user response:

Well, for one, you are not returning the object from the database, you are returning a query. Secondly that query doesn't make logical sense, I think you meant to select a value where the record matches either value.

Yry this:

var pharamacy = db.Pharmacies.Where(x=>x.Adress == cityName || x.Adress == areaName).FirstOrDefault();
  • Related