How show List of string in the ListBox in MVC view

Alma

I am have a table similar to grid that shows all the fields from table. This is my controller:

    public ActionResult Index()
    {
        DAL.DataManager dal = new DAL.DataManager();
        List<LALegalLicensedata> data = new List<LALegalLicensedata>();
        data = dal.get_LA_Get_AllDate();

       return View(data);

    }

and this is my view:

    @model IEnumerable<CSAProject.Models.LALegalLicensedata>


   <table width="100%" class="display" id="example" cellspacing="0">
    <thead>
        <tr>
            <th>Entity</th>
            <th>License Type</th>
            <th>State</th>
            <th>Location</th>
            <th>Data User</th>

        </tr>
    </thead>
    <tbody>
@foreach(var item in Model)
{
<tr>
    <td>@item.Entity</td>
    <td>@item.License_Type</td>
    <td>@item.State</td>
    <td>@item.Location</td>
    <td>@item.dataUser</td>

</tr>

}
    </tbody>

</table>

also in this page I need to show a optionList with checkbox that contains name of the properties from Model, this is my model:

public class LALegalLicensedata
    {

          public int dataID { get; set; }      
          public string dataUser { get; set; }
          public DateTime Create_Date { get; set; }
          public DateTime Modified_Date { get; set; }
          public string Modified_By { get; set; }
          public string Status { get; set; }
}

and this is how I get the properties name from Model:

        LALegalLicensedata model = new LALegalLicensedata();
        List<string> PropertyList =   GetPropertiesNameOfClass(model);


 public List<string> GetPropertiesNameOfClass(object pObject)
    {
        List<string> propertyList = new List<string>();
        if (pObject != null)
        {
            foreach (var prop in pObject.GetType().GetProperties())
            {
                propertyList.Add(prop.Name);
            }
        }
        return propertyList;
    }

I need to show a PropertyList in the option list how I can do that?

This is the javascript and text to show and hide the column. Instead of static text I like to have names from properties and have them in the option list with checkbox.

    $(document).ready(function () {
        var table = $('#example').DataTable({

            "paging": true
        });
        $('a.toggle-vis').on('click', function (e) {
            //e.preventdefault();
            event.preventDefault ? event.preventDefault() : event.returnValue = false;

            //Get the column API object
            var column = table.column($(this).attr('data-column'));



            // Toggle the visibility

            column.visible(!column.visible());

        });

    });

</script>
<div>
    Toggle column: <a class="toggle-vis" data-column="0">Entity</a> - 
    <a class="toggle-vis" data-column="1">License Type</a> - 
    <a class="toggle-vis" data-column="2">State</a> - 
    <a class="toggle-vis" data-column="3">Location</a> - 
    <a class="toggle-vis" data-column="4">Data User</a> - 
    <a class="toggle-vis" data-column="5">Create Date</a> 

</div> 
user3559349

The model you have shown does not match the view you have shown, so assuming your model is in fact (to match the view you have shown)

public class LALegalLicensedata
{
    public string Entity { get; set; }      
    public string License_Type { get; set; }
    public string State { get; set; }
    public string Location { get; set; }
    public string dataUser { get; set; }
}

Then, in the Index() method, add the property names to a ViewBag property

....
ViewBag.PropertyNames = GetPropertiesNameOfClass(new LALegalLicensedata());
return View(data);

I would however recommend that you use a view model with properties List<LALegalLicensedata> Data and List<string> PropertyNames

Then in the view your can loop through the collection to generate you checkboxes

<div>
  @foreach(var name in ViewBag.PropertyNames)
  {
    <label>
      <input type="checkbox" class="toggle-column" checked />
      <span>@name</span>
    </label>
  }
</div>

Then modify your script to handle the click() event of each checkbox

var checkBoxes = $('.toggle-column');
$('.toggle-column').click(function() {
  var index = checkBoxes.index($(this));
  $('tr th').eq(index).toggle();
  $('tr td').eq(index).toggle();
});

Refer this fiddle for how the script works.

Edit

Your current GetPropertiesNameOfClass() method will return the property names which in your case will display "License_Type" for the 2nd property, when I suspect you probably want it to be "License Type" (space instead of underscore). To solve this, add a [Display(Name = "License Type")] to the property, and then you can use the following method

private List<string> GetDisplayNames(object model)
{
  Type type = typeof(model);
  List<string> displayNames = new List<string>();
  foreach (var property in type.GetProperties())
  {
    var attributes = property.GetCustomAttributes(typeof(DisplayAttribute), true);
    if (attributes.Length == 0)
    {
      displayNames.Add(property.Name);
    }
    else
    {
      displayNames.Add((attributes[0] as DisplayAttribute).Name);
    }
  }
  return displayNames;
}

This also means you can use <th>@Html.DisplayNameFor(m => m.License_Type)</th> to generate you table headings rather than hard coding.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to display a list in View in MVC?

From Dev

How to get list in mvc view

From Dev

Show List string in HTML(MVC) using loop

From Dev

How to split string in View in MVC?

From Dev

How do i show a list from a model to another View in ASP.NET MVC 4

From Dev

How to show user control in MVC view

From Dev

How to select a string in list view

From Dev

Converting (Dictionary<string, List<string>>) value).Values)which is a collection to array by IValueConverter to show in a listbox

From Dev

how to show all friends names in list view

From Dev

how to send string list from controller to javascript (present in view) in asp.net mvc

From Dev

how to send string list from controller to javascript (present in view) in asp.net mvc

From Dev

How to use ListBox.Items as Itemsource of a List<String>

From Dev

How to display a list of objects in an MVC View?

From Dev

How to add checkboxes in dropdown list MVC view

From Dev

how to list data order by date in mvc view

From Dev

How to accepts list of objects mvc create view

From Dev

How to render Spring MVC view to a string

From Dev

How to indent string in MVC 5 View

From Dev

Convert strings in ListBox to List<String>

From Dev

how to show MVC4 Directory.GetFiles in view

From Dev

How to show sum using LINQ statement in Grid view of the MVC app

From Dev

How to pass list string to JLabel and show in JFrame

From Dev

How to remove and show there were string duplicates in a list?

From Dev

Viewbag type datetime is null but View show empty string and cannot show data (MVC)

From Dev

Display List in a View MVC

From Dev

Display List in View in MVC

From Dev

Pass a List to View In MVC

From Dev

How to populate a ListBox based on a list of a model in asp.net mvc4?

From Dev

How can i show count down timer on list view items?

Related Related

  1. 1

    How to display a list in View in MVC?

  2. 2

    How to get list in mvc view

  3. 3

    Show List string in HTML(MVC) using loop

  4. 4

    How to split string in View in MVC?

  5. 5

    How do i show a list from a model to another View in ASP.NET MVC 4

  6. 6

    How to show user control in MVC view

  7. 7

    How to select a string in list view

  8. 8

    Converting (Dictionary<string, List<string>>) value).Values)which is a collection to array by IValueConverter to show in a listbox

  9. 9

    how to show all friends names in list view

  10. 10

    how to send string list from controller to javascript (present in view) in asp.net mvc

  11. 11

    how to send string list from controller to javascript (present in view) in asp.net mvc

  12. 12

    How to use ListBox.Items as Itemsource of a List<String>

  13. 13

    How to display a list of objects in an MVC View?

  14. 14

    How to add checkboxes in dropdown list MVC view

  15. 15

    how to list data order by date in mvc view

  16. 16

    How to accepts list of objects mvc create view

  17. 17

    How to render Spring MVC view to a string

  18. 18

    How to indent string in MVC 5 View

  19. 19

    Convert strings in ListBox to List<String>

  20. 20

    how to show MVC4 Directory.GetFiles in view

  21. 21

    How to show sum using LINQ statement in Grid view of the MVC app

  22. 22

    How to pass list string to JLabel and show in JFrame

  23. 23

    How to remove and show there were string duplicates in a list?

  24. 24

    Viewbag type datetime is null but View show empty string and cannot show data (MVC)

  25. 25

    Display List in a View MVC

  26. 26

    Display List in View in MVC

  27. 27

    Pass a List to View In MVC

  28. 28

    How to populate a ListBox based on a list of a model in asp.net mvc4?

  29. 29

    How can i show count down timer on list view items?

HotTag

Archive