0

I have a function here which will makes an ajax call to return data. I want to return data using 5 variables as shown to filter the data.

workarearefs, jurisidcitons and tags will be from a query string in this kind of format:

details?companyid=1&datepreset=lastmonth&jurisdictionref=3&jurisdictionref=6&jurisdictionref=18&s=1&workarearef=2&workarearef=11&workarearef=14

datapreset and companyid is coming from a view which works.

How can I parse the query string into the ajax call then I would like to use linq to filter the results with the refs in the url?

JS:

taxiBriefingShow.click(function (e) {
    e.preventDefault();
    if (!loaded) {
        var companyId = $(this).data('companyid');
        var workAreaRefs = // from querystring, 0 to many: workarearef=1&workarea=2&workarea=99 etc
        var jurisdictions = $(this).data("jurisdictions"); // from querystring, 0 to many: jurisdiction=1&jurisdiction=2&jurisdiction=99 etc
        var tags = $(this).data("tags"); // from querystring, 0 to many: tagref=1&tagref2=2&tagref=99 etc
        var preset = $(this).data("preset");

        var filterModel =
        {
                WorkareaRefs: workAreaRefs,
                Jurisidctions: jurisdictions,
                Tags: tags,
                Preset: preset
        }

        $.ajax({
            data: JSON.stringify(filterModel),
            url: '/api/track/v1/taxibriefing?companyId=' + companyId + '&preset=' + preset + '&workarearefs=' + workAreaRefs + '&tags=' + tags + '&jurisdictions=' + jurisdictions, 
            async: false,
            success: generateTaxiLists
        });
    }
    togglePanel();
});

I am setting properties to these with these applied filters in the model:

    public IList<TrackFilterGenericRef> Workareas { get; set; }

    public IList<TrackFilterGenericRef> Jurisdictions { get; set; }

    public IList<TrackFilterGenericRef> Tags { get; set; }

To set these properties in the API Controller: The part from var workareas = workareaRefs.Split(',') is wrong.

    [HttpPost]
    public async Task<TaxiContainerModel> Post([FromBody] WidgetConfigurationRequestVM data, int companyId, string workareaRefs, string tags, string jurisidctions, DatePreset preset)
    {

        if (!IsCompanyAllowed(data.CompanyId))
        {
            throw new HttpResponseException(HttpStatusCode.Forbidden);
        }

        var MLId = GetMLID(data.CompanyId);
        if (string.IsNullOrWhiteSpace(MLId))
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }

        if (data.Configuration.IsNullOrEmpty())
        {
            throw new HttpResponseException(HttpStatusCode.BadRequest);
        }

        var Workareas = workareaRefs.Split(',');
        var Tags = tags.Split(',');
        var Jurisidctions = jurisidctions.Split(',');

        var r = new TrackDataFilter
        {
            DatePreset = preset,
            Workareas = new List<TrackFilterGenericRef>
            {
                new TrackFilterGenericRef
                {

                    Ref = 2, Type = Enums.ContentTypes.Workarea
                }
            },
        };

        return await taxiBriefingService.GenerateBriefingAsync(data.CompanyId, MLId, data.Notes == null? "" : data.Notes, LexUser.FirmRef, data.Configuration.Select(x=>( x.Row, x.Col, (TaxiWidgetSize)x.Size, (TaxiWidgetType)x.Type)), r);
    }
}

workareas, jurisdictions, and tags should contain the values in workareaRefs/jurisdictions/tags from query string stored in the variable named r. Can anyone help me achieve this?

N8888
  • 670
  • 2
  • 14
  • 20
Karim Ali
  • 61
  • 2
  • 10
  • Are you using MVC? If so, post your controller method. –  Jul 30 '18 at 15:38
  • Using API controller, I've added thanks – Karim Ali Jul 30 '18 at 15:51
  • Your title and your tags don't really match. If you're trying to parse query strings in JS, that question has been asked before: [How can I get query string values in JavaScript?](https://stackoverflow.com/q/901115). But you don't have the `javascript` tag there, so should we assume you actually want to get the items from the query string in your API method? If so, there's a `[FromQueryString]` attribute you can apply to arguments. – Heretic Monkey Jul 30 '18 at 15:59
  • There's no data attribute being set for workAreaRefs in your ajax call. You have commented out text. –  Jul 30 '18 at 16:05
  • dickrichie sorry it's in there now, thanks – Karim Ali Jul 30 '18 at 16:26

0 Answers0