CheckBoxList در ASP.NET MVC
نویسنده: وحید نصیری
تاریخ: ۱۳۹۱/۰۲/۱۰ ۲۰:۴۳:۰۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
@{
ViewBag.Title = "Index";
}
<h2>
Index</h2>
@using (Html.BeginForm())
{
<input type='checkbox' name='Result' value='value1' />
<input type='checkbox' name='Result' value='value2' />
<input type='checkbox' name='Result' value='value3' />
<input type="submit" value="submit" />
}
using System.Web.Mvc;
namespace MvcApplication21.Controllers
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string[] result)
{
return View();
}
}
}
@helper CheckBoxList(string name, List<System.Web.Mvc.SelectListItem> items)
{
<div class="checkboxList">
@foreach (var item in items)
{
@item.Text
<input type="checkbox" name="@name"
value="@item.Value"
@if (item.Selected) { <text>checked="checked"</text> }
/>
< br />
}
</div>
}
using System.Collections.Generic;
using System.Web.Mvc;
namespace MvcApplication21.Controllers
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
ViewBag.Tags = new List<SelectListItem>
{
new SelectListItem { Text = "Item1", Value = "Val1", Selected = false },
new SelectListItem { Text = "Item2", Value = "Val2", Selected = false },
new SelectListItem { Text = "Item3", Value = "Val3", Selected = true }
};
return View();
}
[HttpPost]
public ActionResult GetTags(string[] tags)
{
return View();
}
[HttpPost]
public ActionResult Index(string[] result)
{
return View();
}
}
}
@{
ViewBag.Title = "Index";
}
<h2>
Index</h2>
@using (Html.BeginForm())
{
<input type='checkbox' name='Result' value='value1' />
<input type='checkbox' name='Result' value='value2' />
<input type='checkbox' name='Result' value='value3' />
<input type="submit" value="submit" />
}
@using (Html.BeginForm(actionName: "GetTags", controllerName: "Home"))
{
@Helpers.CheckBoxList("Tags", (List<SelectListItem>)ViewBag.Tags)
<input type="submit" value="submit" />
}
@if (item.Selected) { <text>checked="checked"</text> }
//this method is in "UserController" class that select all available roles form database
[NonAction]
public List<SelectListItem> GetRoleList()
{
var usrMgmt = new UserManagement();
var RoleList = new List<SelectListItem>();
foreach (KeyValuePair<string, string> pair in usrMgmt.GetAllRoles())
{
RoleList.Add(new SelectListItem { Text = pair.Value, Value = pair.Key, Selected = false });
}
return RoleList;
}
//---------------------------------------------------------------------------
//this method is in "UserController" class that use for editing a user
[HttpGet]
public ActionResult Edit(string Username)
{
var roles = GetRoleList();
UserManagement usrMgmt = new UserManagement();
var query = usrMgmt.Select(Username);
foreach (string role in query.Roles)
{
int index = roles.FindIndex(x=>x.Value == role);
roles[index].Selected = true;
}
ViewBag.Roles = roles;
return View(query);
}
//--------------------------------------------------------------
//this method is in "UserManagement" class in model layer, return a
//dictionary<string,string> that first string is english role name and second one is persian role //name
public Dictionary<string,string> GetAllRoles()
{
Dictionary<string, string> roles = new Dictionary<string, string>();
var db = new SamaEntities();
string query = "";
string[] roleNames = Roles.GetAllRoles();
foreach (string role in roleNames)
{
query = db.aspnet_Roles.Single(x => x.RoleName == role).Description;
roles.Add(role, query);
}
return roles;
}