Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6984381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:32:32+00:00 2026-05-27T18:32:32+00:00

I have a list in Controller (MVC) public CompanyController() { _company = new List<CompanyModel>{

  • 0

I have a list in Controller (MVC)

public CompanyController() {
         _company = new List<CompanyModel>{
            new CompanyModel{Name="A 1", Email="a1@company.com", Group="Group1"},
            new CompanyModel{Name="A 2", Email="a2@company.com", Group="Group2"},
            new CompanyModel{Name="A 3", Email="a3@company.com", Group="Group3"},
            new CompanyModel{Name="B 1", Email="b1@company.com", Group="Group1"},
            new CompanyModel{Name="C 1", Email="c1@company.com", Group="Group2"},
            new CompanyModel{Name="D 1", Email="d1@company.com", Group="Group4"},
            new CompanyModel{Name="E 1", Email="e1@company.com", Group="Group5"},
            new CompanyModel{Name="E 2", Email="e2@company.com", Group="Group6"},
            new CompanyModel{Name="F 1", Email="f1@company.com", Group="Group6"},
            new CompanyModel{Name="G 1", Email="b1@company.com", Group="Group5"},
            new CompanyModel{Name="H 1", Email="b1@company.com", Group="Group6"},
            new CompanyModel{Name="I 1", Email="b1@company.com", Group="Group3"},
            new CompanyModel{Name="J 1", Email="b1@company.com", Group="Group7"},
            new CompanyModel{Name="K 1", Email="b1@company.com", Group="Group6"},
            new CompanyModel{Name="L 1", Email="b1@company.com", Group="Group1"},
            new CompanyModel{Name="M 1", Email="b1@company.com", Group="Group2"},
            new CompanyModel{Name="N 1", Email="b1@company.com", Group="Group2"},
            new CompanyModel{Name="O 1", Email="b1@company.com", Group="Group4"},
            new CompanyModel{Name="P 1", Email="b1@company.com", Group="Group5"},            
            new CompanyModel{Name="R 1", Email="b1@company.com", Group="Group5"},
            new CompanyModel{Name="S 1", Email="b1@company.com", Group="Group8"},
            new CompanyModel{Name="T 1", Email="b1@company.com", Group="Group8"},
            new CompanyModel{Name="U 1", Email="b1@company.com", Group="Group1"},
            new CompanyModel{Name="V 1", Email="b1@company.com", Group="Group2"},
            new CompanyModel{Name="X 1", Email="b1@company.com", Group="Group4"},
            new CompanyModel{Name="Y 1", Email="b1@company.com", Group="Group3"},
            new CompanyModel{Name="Z 1", Email="b1@company.com", Group="Group3"},
            new CompanyModel{Name="W 1", Email="b1@company.com", Group="Group6"},
         };
      }

which I sent via JSON to JQuery as:

objectCompany = null;
data = "";

$.ajax({
         url: '/Company/List',
         dataType: 'json',
         contentType: 'application/json; charset=utf-8',
         success: function (msg) {
            data = msg;
         },
         complete: function () {
            objectCompany = data;
         }
      });

I want to append to my select tag <select name="filters" id="filters"></select> all options which has Group values without repeating the similar values.

UPDATE

if (objectCompany != null) {
     $("#filters").empty();
     $("#filters").html("<option value='all' selected='selected'>All groups</option>");
     var arrayUnique = [];
     for (var i = 0; i < objectCompany .length; i++) {
        for (var j = i + 1; j < objectCompany .length; j++) {
           if (objectCompany [i].Group === objectCompany [j].Group) {
              j = ++i;
           }
        }
        arrayUnique.push(objectCompany [i].Group);
     }

     arrayUnique = arrayUnique.sort();

     $.each(arrayUnique, function (k, v) {
        $("#filters").append("<option value='" + v + "'>" + v + "</option>");
     });
      }

Update: Now it works 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:32:32+00:00Added an answer on May 27, 2026 at 6:32 pm

    Why struggling with javascript? Why don’t you use a view model? Why don’t you prepare the data from your controller action so that all that the view has to do is display it? What’s the purpose of wasting your bandwidth with some repeated groups in the JSON?

    public ActionResult List()
    {
        var model = _company.Select(x => x.Group).Distinct();
        return Json(model, JsonRequestBehavior.AllowGet);
    }
    

    Now it’s simple:

    var filters = $('#filters');
    filters.empty();
    filters.append(
        $('<option/>', {
            value: 'all',
            selected: 'selected',
            text: 'All groups'
        })
    );
    $.each(objectCompany, function (index, group) {
        filters.append(
            $('<option/>', {
                value: group,
                text: group
            })
        );
    });
    

    UPDATE:

    If you want to do this filtering on the client side you could define a function which will get the distinct elements:

    $.extend({
        distinct : function(arr, selector) {
           var result = [];
           $.each(arr, function(index, value) {
               var item = selector(value);
               if ($.inArray(item, result) == -1) {
                   result.push(item);
               }
           });
           return result;
        }
    });
    

    and then:

    var filters = $('#filters');
    filters.empty();
    filters.append(
        $('<option/>', {
            value: 'all',
            selected: 'selected',
            text: 'All groups'
        })
    );
    var groups = $.distinct(objectCompany, function(item) { return item.Group; });
    
    $.each(groups, function (index, group) {
        filters.append(
            $('<option/>', {
                value: group,
                text: group
            })
        );
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to asp.net mvc. I have index method with parameter id : public
I would like to have the following signature of a MVC controller. public ActionResult
I have a Spring MVC web application with conroller like below : @Controller public
I have list of objects which are returned from mvc controller as Json. I'm
I have the problems with pass list of string into controller in mvc 3
i have this model on mvc: public class User { public string Name {
I have the following where I'm trying to send list/array to MVC controller method:
Let's say I have a simple ASP MVC3 list controller, with an add method,
so i have a Parents controller with list and edit view for it (to
I have controller PlayerController and actions inside: View , Info , List . So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.