簡體   English   中英

在ASP MVC上按Dropdownlist排序

[英]Sorting by Dropdownlist on asp mvc

您好,我想使用viewbag通過selectedId Category對我的索引頁進行排序。 我已經在使用linq並實現了搜索按鈕,但是過濾類別不起作用。

控制器上的此索引方法

 public ActionResult Index(string Search, int Ca ) { //var c = User.Identity.GetUserId(); string c = System.Web.HttpContext.Current.User.Identity.GetUserId(); //int e = int.Parse(Request.Form["Ca"].ToString()); var test = (from s in db.Articles where (s.UserId == c) where (s.titre.Contains(Search)) where (s.Idc == int.Parse (Request["Ca"].ToString())) select s ).Distinct().ToList(); ViewBag.Ca = new SelectList(db.Categories, "Id", "libelle"); return View(test.ToList()); } 

謝謝

為了實現jQuery中的目標,您可以采用以下方式進行處理。

[ApiController]
[Route("api/ux/input/[controller]")]
public class SelectController : ControllerBase
{
     [HttpGet("samples")]
     public async Task<JsonResult> Get() => new JsonResult(await sampleService.RetrieveSampleEntity());
}

然后,在您的視圖內部應該具有與此類似的代碼。

$(document).ready(function() {
     loadAndOrderSampleSelect();
});

function loadAndOrderSampleSelect() {
     axios.get('/api/ux/input/select/samples').then((response) => {
          let samples = sortSelectByCategory(response.data);
          for(var index = 0; index < samples.length; index++) {
               $('#drpSamples').append('<option value="' + samples[index].id + '">' + samples[index].name + '</option>');
          }
     });
}

function sortSelectByCategory(items) {
     return items.sort(function(a, b) {
           return (b['category'] > a['category']) ? 1 : ((b['category'] < a['category']) ? -1 : 0);
     });
}

排序可能無法正常進行,我假設您在頁面上有一個id為drpSamples ,但這應該為您指明使用JavaScript而不是服務器端邏輯進行操作的正確方向。 盡管如此,如果您位於Get內的服務器上,則只需執行sampleService.RetrieveSamples().OrderBy(c => c.Category);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM