Marius Vasile

Marius Vasile

  • 1.4k
  • 657
  • 7.9k

asp.net core get additional info with json and jquery

Jan 30 2021 5:28 AM
I have a select built on json and jquery but the data is concatenated
 
json
  1. public async Task<JsonResult> OnGetAssetAsync(string id)  
  2.         {  
  3.   
  4.             var assets = await _context.WOAssetAssets.Where(s => s.WOALId == id)  
  5.                                       .Select(s => new SelectListItem  
  6.                                       {  
  7.                                           Value = s.WOAId.ToString(),  
  8.                                           Text = s.AssetID + " " + "/" + " " + s.AssetName  
  9.                                       }).ToListAsync();  
  10.   
  11.             return new JsonResult(assets);  
  12.         }  
jquery
  1. $(function () {  
  2.     $("#Asset").on("change", function () {  
  3.         var categoryId = $(this).val();  
  4.         $("#Equipment").empty();  
  5.         $("#Equipment").append("<option value=''>--Select Equipment--</option>");  
  6.         $.getJSON(`?handler=Equipment&id=${categoryId}`, (data) => {  
  7.             $.each(data, function (i, item) {  
  8.                 $("#Equipment").append(`<option value="${item.value}"> ${item.text}</option>`);  
  9.             });  
  10.             $('#Equipment').selectpicker({ liveSearch: true });  
  11.         });  
  12.     });  
  13. });  
and I want to get separate data with 
 
json
  1. public async Task<JsonResult> OnGetAssetDetailsAsync(string id)  
  2.         {  
  3.             var asset = await _context.WOAssetAssets.Where(s => s.WOAId == id).SingleOrDefaultAsync();  
  4.             return new JsonResult(asset.AssetName, asset.AssetManufacturer);  
  5.         }  
and updated jquery
 
  1. $(function () {  
  2.     $("#Asset").on("change", function () {  
  3.         var categoryId = $(this).val();  
  4.         $("#Equipment").empty();  
  5.         $("#Equipment").append("<option value=''>--Select Equipment--</option>");  
  6.         $.getJSON(`?handler=Equipment&id=${categoryId}`, (data) => {  
  7.             $.each(data, function (i, item) {  
  8.                 $("#Equipment").append(`<option value="${item.value}"> ${item.text}</option>`);  
  9.             });  
  10.             $('#Equipment').selectpicker({ liveSearch: true });  
  11.         });  
  12.         $.getJSON(`?handler=AssetDetails&id =${categoryId}`, (data) => {  
  13.             $("#AssetName").append("asset.AssetName");  
  14.             $("#AssetManufacturer").append("asset.AssetManufacturer");  
  15.         });  
  16.     });  
  17. });  
but is not working. How should I do it? 

Answers (10)