当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to show a list of checkbox from the base in ASP MVC and Javascript

问题描述:

I am developing an ASP .Net MVC 3 application using C# and SQL Server 2005.

I am using also Entity Framework and Code First Method.

My problem is to display a list of checkbox of values which are loadinf from a table in the base.

The list of checkbox depends on the value of the item selected in a DropDownList.

I try to do this using Javascript :

<script type="text/javascript">

$.post("/ProfileGa/Application", { term: "" + $("#Txt").val() + "SelectedGenre" },

function (html)

{

//construct check boxes with this data elements.

$('#resultDiv').append("<input type='checkbox' id='checkbox" + html[i].Id + "'" + "/>" + html[i].title);

}

</script>

This is the View :

<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>

and this is the Controller :

[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);

}

and this is the model :

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>();

}

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: