Hi all..
I have a page frmLocationMaster.aspx. ..
On this, I am having 2 checkboxlist controls -
1. checkboxlistusers
2. checkboxlistlocation
Uitlity -
Users must get access to specific locations only ..
Eg -
User Table
| ID | User |
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
| 5 | E |
| ID | Location |
| 1 | UP |
| 2 | Bihar |
| 3 | Andhra Pradesh |
| 4 | Karnataka |
| 5 | Maharashtra |
| 6 | West Bengal |
| 7 | Gujarat |
| 8 | Rajasthan |
Suppose, Users - A,B,C must have access to Location - UP, Rajasthan and Bihar.
Then, user will select - users -A,B,C From userscheckboxlist and Location - UP, Rajastahn,Bihar from LocationCheckboxlist.
On button click event - Following data must be inserted -
| UserID | Location |
| A | UP |
| B | UP |
| C | UP |
| A | Rajasthan |
| B | Rajasthan |
| C | Rajasthan |
| A | Bihar |
| B | Bihar |
| C | Bihar |
Please guide ..
Brijesh RathorPosted Nov 29, 2016, 10:08 AM
MayankPosted Nov 29, 2016, 6:30 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ValidationsMvc.Models
{
public class CheckBoxClass
{
public List
public List
public string LocationList { get; set; }
public string UserList { get; set; }
}
public class Users
{
public int Id { get; set; }
public string UserName { get; set; }
}
public class Location
{
public int Id { get; set; }
public string LocationName { get; set; }
}
}
Controller code:
public ActionResult CheckBoxListSelection()
{
var user = new Users
{
Id= 1,
UserName = "A"
};
var user2 = new Users
{
Id = 2,
UserName = "B"
};
var user3 = new Users
{
Id = 3,
UserName = "C"
};
List
list.Add(user);
list.Add(user2);
list.Add(user3);
var loc = new Location
{
Id = 1,
LocationName = "UP"
};
var loc2 = new Location
{
Id = 2,
LocationName = "Delhi"
};
var loc3 = new Location
{
Id = 3,
LocationName = "Bihar"
};
List
lst.Add(loc);
lst.Add(loc2);
lst.Add(loc3);
var model = new CheckBoxClass
{
Usr = list,
Loc=lst
};
return View(model);
}
[HttpPost]
public ActionResult CheckBoxListSelection(CheckBoxClass model)
{
//Here you will get the location and user in list u can split it by split function and save in DB
return RedirectToAction("CheckBoxListSelection");
}
View:
@model ValidationsMvc.Models.CheckBoxClass
@*~/Models/CheckBoxClass.cs*@
@{
ViewBag.Title = "CheckBoxListSelection";
}
CheckBoxListSelection
@using (Html.BeginForm("CheckBoxListSelection", "Home", FormMethod.Post))
{
@foreach (var items in Model.Usr as List
{
@items.UserName
}
@Html.HiddenFor(M => M.UserList, new { id = "userIdLst" })
@foreach (var items in Model.Loc as List
{
@items.LocationName
}
@Html.HiddenFor(M => M.LocationList, new { id = "locIdLst" })
}
If this resolve your issue please mark as answer
Manas MohapatraPosted Nov 29, 2016, 5:22 AM
Suvendu Shekhar GiriPosted Nov 29, 2016, 4:56 AM