我正在使用C#和SQL Server 2005开发ASP .Net MVC 3应用程序。
我也在使用Entity Framework和Code First Method。
我有一个'Application'视图,其中包含DropDownList和Table。
DropDownList的项目以列表的形式从基础(Table = Genre)加载。
根据DropDownList中的选定项目,我希望在表格中以 CheckBox 的形式在基础中显示一些值(列表)。
我尝试过CheckBoxFor,但它无效。
这是视图:
<div>
<%:Html.Label("Type :")%><%: Html.DropDownListFor(model => model.SelectedGenre, Model.GenreItems)%>
</div>
<table border = "transparent">
<tr>
<th>
</th>
</tr>
<% foreach (var item in Model.FaItems) { %>
<tr>
<td>
<%: Html:CheckBoxFor (modelItem => item.Nom_Famille) %>
</td>
</tr>
<% } %>
</table>
这是控制器:
[HttpGet]
public ActionResult Application(Genre genre)
{
var vv = new FlowViewModel();
vv.GenreItems = new SelectList(db.Genres.ToList(), "ID_G", "ID_G");
if (vv.SelectedGenre == "Famille")
{
vv.FaItems = db.Familles.ToList();
}
else if (vv.SelectedGenre == "Sous Famille")
{
vv.SFItems = db.Sous_Familles.ToList();
}
return View(vv);
}
这是模型:
public class FlowViewModel
{
[Key]
public string IDv { get; set; }
public List<Famille> FaItems { get; set; }
public List<Sous_Famille> SFItems { get; set; }
[NotMapped]
public SelectList GenreItems { get; set; }
public string SelectedGenre { get; set; }
public FlowViewModel()
{
FaItems = new List<Famille>();
SFItems = new List<Sous_Famille>();
}
}
注意:
我想选择列表中的一些值(检查它们)并在基数中记录,如果你有除复选框之外的任何其他建议,那将是非常有用的。 感谢。
答案 0 :(得分:2)
为了在复选框中显示用户角色我做了FOllowing,你可以找到自己的方式。不同的是,我在剃刀中做了
@{
var roles = (SimpleRoleProvider)Roles.Provider;
string[] strroles = roles.GetAllRoles();
string[] userorles = roles.GetRolesForUser(Model.UserName);
foreach (string strrole in strroles)
{
bool isinrol = false;
foreach (string struserroles in userorles)
{
if (strrole == struserroles)
{
isinrol = true;
}
}
if(isinrol)
{
<input type="checkbox" id="roles[]" name="roles" value="@strrole" checked="checked" /> @strrole <br />
}
else
{
<input type="checkbox" id="roles[]" name="roles" value="@strrole"/> @strrole <br />
}
}
}