locked
How to make a comma-separated list of all the students (name + surname) who enrolled in the course? RRS feed

  • Question

  • User-2122129078 posted
    public class Student
    {
        public int ID { get; set; }
        public string LastName { get; set; }
        public string FirstMidName { get; set; }
        public DateTime EnrollmentDate { get; set; }
    
        public virtual ICollection<Enrollment> Enrollments { get; set; }
    }

    and my view looks like, I know hpw to add a column, but I dont know how to show students Name + LastName

    <table class="table">
        <tr>
            <th>
                @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm })
            </th>
            <th>
                First Name
            </th>
            <th>
                @Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm })
            </th>
            <th></th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.LastName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FirstMidName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EnrollmentDate)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
                @Html.ActionLink("Details", "Details", new { id=item.ID }) |
                @Html.ActionLink("Delete", "Delete", new { id=item.ID })
            </td>
        </tr>
    }
    Monday, August 11, 2014 9:01 AM

Answers

  • User281315223 posted

    As MetalAsp.Net mentions you can use an additional property to return a formatted name for a particular student (or enrollment). You could then simply output this value by looping through your collection of students :

    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.FullName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EnrollmentDate)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
                @Html.ActionLink("Details", "Details", new { id=item.ID }) |
                @Html.ActionLink("Delete", "Delete", new { id=item.ID })
            </td>
        </tr>
    }

    If you needed to actually build a single comma-delimited string containing all of your students names (Last,First) then you could use the String.Join() method and pass in your collection of Students :

    // Build a comma-delimited list of all of your students
    var students = String.Join(" , ",YourStudentsCollection.Select(s => s.FullName).ToArray());

    which would yield something like :

    "LastName1, FirstName1 , LastName2, FirstName2, LastName3, FirstName3 , ... "

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 11, 2014 9:44 AM

All replies

  • User-1360095595 posted

    Add a new property to your entity:

    public string FullName
    {
       get { return (LastName + ", " + FirstMidName); }
    }

    Monday, August 11, 2014 9:14 AM
  • User281315223 posted

    As MetalAsp.Net mentions you can use an additional property to return a formatted name for a particular student (or enrollment). You could then simply output this value by looping through your collection of students :

    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.FullName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EnrollmentDate)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
                @Html.ActionLink("Details", "Details", new { id=item.ID }) |
                @Html.ActionLink("Delete", "Delete", new { id=item.ID })
            </td>
        </tr>
    }

    If you needed to actually build a single comma-delimited string containing all of your students names (Last,First) then you could use the String.Join() method and pass in your collection of Students :

    // Build a comma-delimited list of all of your students
    var students = String.Join(" , ",YourStudentsCollection.Select(s => s.FullName).ToArray());

    which would yield something like :

    "LastName1, FirstName1 , LastName2, FirstName2, LastName3, FirstName3 , ... "

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 11, 2014 9:44 AM