Sorting Table by Its GroupName, Where Data is Fetc

2019-08-28 02:29发布

问题:

I need to have a View which looks something like this:

[+] Group Name1 //this is supposed to be an accordion menu

Sub Name| Scale1| Scale2| Scale3| Scale4 //this is supposed to be table header

Abc | O | O | O | O // 'Abc' is text and ' O ' are radio button

XYZ | O | O | O | O // Same as above row

[+] Group Name2
Sub Name | Scale1 | Scale2 | Scale3 | Scale4 //Same as last table

Pqr | O | O | O | O
Mno | O | O | O | O

I am able to get the table on click of [+]. And, I am generating the list of the rows, using a foreach loop which fetches values from database table. But, the issue I am facing is that, I am able to get the values in one table and all the values are listed in it e.g : abc,xyz,..,pqr,mno,... But I need it to SORT IT according to its 'Group Name', though I am able to group it. Please suggest how do I bind it with view. Also, Can anyone please suggest me, how do I work with the radiobuttons. I need to store the selected radiobutton's value in database table for this.

Here is my view:

<div class='toggle'>
      <h2>&nbsp;</h2>
          <h2>lList</h2>
      <div class="togglebox">

    <table width="50%">
    <br />
    <br />
     <tr>
     <th>
      Group Name
     </th>
     <th>
     Sub Name
     </th>
     <th>
     Scale1
     </th>
     <th>
     Scale2
     </th>
     <th>
     Scale3
     </th>
     <th>
     Scale4
     </th>
     </tr>

     <% int counter = 0; %>
      <% foreach (var item in Model.skills)
         { %>

     <tr>
        <td>
       <%=item.GroupName%>
       </td>
        <td>
       <%=item.SubName%>
       </td>
       <td>
       <%--<input type="radio" name="scale<%:counter %> />--%>
       <%:Html.RadioButton("scale" + counter, 0, item.Scale)%>
       <%--<%=Html.RadioButtonFor(m => item.Scale, "Scale1", new { @name = "scale" + counter })%>--%>
       </td>
       <td>
       <%:Html.RadioButton("scale" + counter, 1, item.Scale)%>
      <%-- <%=Html.RadioButtonFor(m => item.Scale,"Scale2", new { @name = "scale" + counter})%>--%>
       </td>
       <td>
        <%:Html.RadioButton("scale" + counter, 2, item.Scale)%>
       <%--<%=Html.RadioButtonFor(m => item.Scale,"Scale3", new { @name = "scale" + counter})%>--%>
       </td>
       <td>
        <%:Html.RadioButton("scale" + counter, 3, item.Scale)%>
       <%--<%=Html.RadioButtonFor(m => item.Scale,"Expert", new {@name = "scale" + counter})%>--%>
       </td>
     </tr>
       <% counter++;
         } %>


     </table>

Here is my model:

public class Skill
{
    [DisplayName("Sub Name")]
    [Required]
    public string SubName { get; set; }


    [DisplayName("Group Name")]
    [Required]
    public string GroupName { get; set; }

    public bool Scale { get; set; }



}

Here is my DataAccessLayer:

        public static DataTable GetAllList(string ID)
    {
        SqlConnection con = new SqlConnection(connect);
        con.Open();
        SqlCommand cmd = new SqlCommand("spGetSkillsList", con);
        cmd.Parameters.AddWithValue("@ID", 1);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds.Tables[0];
    }

here is my business layer

 public static SkillMetricsViewModel GetAllList(string ID)
    {
        ListViewModel viewModel = new ListViewModel();
        DataTable dt = DAL.GetAllList(ID);

        List<Skill> skills = new List<Skill>();
        foreach (DataRow row in dt.Rows)
        {
            Skill skill = new Skill() { GroupName = row["GroupName"] != null ? row["GroupName"].ToString() : string.Empty, SubName = row["SubName"] != null ? row["SubName"].ToString() : string.Empty};
            skills.Add(skill);
        }
        IEnumerable<IGrouping<string, Skill>> test = skills.GroupBy(i => i.SkillGroupName);
        viewModel.skills = skills.GroupBy(x => x.SkillGroupName).SelectMany(r => r).ToList();
       return viewModel;

    }

Please Help me in this. Thanks in advance for any kind of help.

回答1:

So if you just need to to always sort by GroupName then in your business layer you could just use linq to do something like

viewModel.skills = skills.OrderBy(x => x.GroupName)