0

Hello i want to sort my index page by selectedId Category using viewbag . I'm already use linq and implement search button is work but filtering category doesn't work.

This Index method on controller

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());          
    }

thanks

mor
  • 71
  • 1
  • 1
  • 7
  • You really should not be using a `ViewBag`, since the data is dynamic and is not specified until runtime. Makes debugging a real nightmare, you would be better off instantiating a model or combining that with another model in a MVVM approach. My two cents, you could also simply use `.OrderBy(category => category.Id)` to order the list. – Greg Mar 12 '19 at 16:49
  • I want to do that with jquery but i dont know how implement that can help please thanks – mor Mar 12 '19 at 16:53
  • 1
    If you want to do it with javascript first you have to save the variable on a javascript array at your view, like this – Jordi Jordi Mar 12 '19 at 17:42
  • Plus if you are using JavaScript, keep in mind depending on how much content you have you won't be able to add and sort the contents for the dropdown until the DOM is ready. So you may experience quirky UX behavior that you'll have to address if other variables come into play. – Greg Mar 12 '19 at 18:08
  • One solution, would be to do a Controller with a `JsonResult` as the return type. That way you can have the server return the model, then you can preemptively order on server or once you promise is returned. Then dump add to the input, since you would be doing on ready and through Ajax it would be async back to server. Not familiar with architecture, but a suggestion. – Greg Mar 12 '19 at 18:11

1 Answers1

0

To accomplish your goal in jQuery, you could do an approach in the following manner.

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

Then inside your view you should have code similar to this.

$(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);
     });
}

The sorting may not work right and I assume you have a select with an id on the page called drpSamples, but this should point you in the proper direction to do it with JavaScript instead of server side logic. All though, if you are on server inside your Get you could simply do sampleService.RetrieveSamples().OrderBy(c => c.Category);

Greg
  • 11,302
  • 2
  • 48
  • 79
  • Thanks greg a i apreciate your answer but i use asp.net mvc 5 on my project and dont know how to update it and add api for making that work – mor Mar 14 '19 at 15:29
  • @mor Web Api and MVC are virtually identical, the difference is primarily to routing and MVC has view engine in it. That is why in Core they were merged. – Greg Mar 14 '19 at 20:36
  • My answer answers your question if you research MVC a bit. – Greg Mar 15 '19 at 13:08