根据它的组名排序表,其中数据是使用MVC 2.0从数据库中获取的
本文关键字:MVC 获取 数据库 数据 排序 | 更新日期: 2023-09-27 18:08:44
我需要有一个视图,看起来像这样:
[+] Group Name1//这应该是一个手风琴菜单
子名称| Scale1| Scale2| Scale3| Scale4//这应该是表头
Abc | O | O | O//'Abc'为文本,' O '为单选按钮
XYZ | O | O | O | O//与上一行相同
[+]组名e2
子名称| Scale1 | Scale2 | Scale3 | Scale4//与上一个表相同
Pqr | O | O | O | O
Mno | O | O | O | O
我可以得到表点击[+]。并且,我正在生成行列表,使用foreach循环从数据库表中获取值。但是,我面临的问题是,我能够在一个表中获得值,所有值都在其中列出,例如:abc,xyz,…,pqr,mno,…但是我需要它根据它的"组名"对它进行排序,尽管我能够对它进行分组。请建议我如何将它与视图绑定。还有,谁能建议我,我如何使用单选按钮。我需要将选中的单选按钮的值存储在数据库表中。
这是我的观点:
<div class='toggle'>
<h2> </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>
这是我的模型:
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; }
}
这是我的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];
}
这是我的业务层
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;
}
请帮我一下。谢谢你的帮助。
如果你总是需要按GroupName排序那么在业务层你可以使用linq来做类似
viewModel.skills = skills.OrderBy(x => x.GroupName)