How to show a list of checkbox according to a Drop

2019-09-18 20:47发布

问题:

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.

I have a view 'Application' which contain a DropDownList and a Table.

The items of DropDownList are being loaded from the base (Table=Genre) in form of a list.

According to the selected item in the DropDownList, I want didplay some values (list) in the base in form of CheckBox in the table.

I tried CheckBoxFor but it's not working.

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

NOTE :

I want choose some values of the list (check them) and record that in the base, if you have any another suggestion except checkbox, that's will be very useful. Thanks.

回答1:

For Showing User Roles in checkboxes I did the FOllowing, you can find your way. the difference is that, I did in Razor

             @{
                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 />
                    }
                }
              }