弄了好久,只解决了多个checkbox从数据库中恢复的问题
时间呀 我的时间
controller里的代码:
IQueryable<string> specieslist = from c in edc.Species select c.SName;
Users user = edc.Users.SingleOrDefault(p => p.UName == User.Identity.Name);
IQueryable<int> interestedspecieslisttemp = from c in edc.UsersSpecies where c.UID==user.UID select (int)c.SID;
IDictionary<string, string> interestedspeciesbool =new Dictionary<string,string>();
foreach (string species in specieslist)
{
interestedspeciesbool.Add(species,"");
}
foreach (int i in interestedspecieslisttemp)
{
Species temp = edc.Species.SingleOrDefault(p => p.SID == i);
interestedspeciesbool[temp.SName] = "checked";
}
ViewData["interestedspeciesbool"] = interestedspeciesbool;
view里面的:
<%foreach (KeyValuePair< string,string> species in (Dictionary<string,string>)ViewData["interestedspeciesbool"] ){%>
<%=species.Key %> <input id="<%=species.Key %>" type="checkbox" name="<%=species.Key %>" value="<%=species.Key %>" <%=species.Value=="checked"?"checked=\"checked\"":"" %> />
<%
} %>
各位大侠有没有比较简便的方法?