jquery - Set user roles in mvc 4 -


hi,

i have 1 page in page have shown details users data. if had login using admin role details visible , login using users role details going non visible

for reference had add image:-

enter image description here

controller:-

 public actionresult editableuserdetails()         {             viewbag.username = "welcome" + ":" + " " + session["username"].tostring();             viewbag.usertypeid = session["usertypeid"].tostring();             //list<editableuserdetails> editableuserdetailsobject = new list<editableuserdetails>();             var linq = (from db in entityobj.users                         db.isactive == true                         select new editableuserdetails                         {                             userid = db.userid,                             username = db.username,                             password = db.password,                             category = db.category                         }).tolist();             var data = linq.tolist();             return view(data);         } 

model class:-

public class editableuserdetails {     public string username { get; set; }     public int userid { get; set; }     public string password { get; set; }     public string category { get; set; } } 

enter image description here

if click "person" box show users details have set permission admin role not users role..

is there way implement depends on login details. thank you.

view page:-

@model ienumerable<stg_test2.models.editableuserdetails>  @{     layout = "~/views/shared/_layout.cshtml";     @scripts.render("~/bundles/jqueryui")      } <input type="submit" value="person" id="sbtpersondetails" /> <input type="submit" value="volunteer" id="sbtvolunteerdetails" /> <input type="submit" value="potential volunteer" id="sbtpotentialvolunteerdetails" /> <input type="submit" value="child sponcers" id="sbtchildsponcerdetails" /> <input type="submit" value="children details" id="sbtchildren" /> <input type="submit" value="year review report" id="sbtyearreviewreport" />  <table id="tblusers">     <thead>         <tr style="background-color: #7ac0da;">              <th>username</th>             <th>password</th>             <th>category</th>             <th class="td-img">process</th>          </tr>     </thead>     <tbody>         @foreach (var @item in model)         {             <tr>                 <td>@item.username</td>                 <td>@item.password</td>                 <td>@item.category </td>                 <td class="td-img">                     <img src="../images/edit.png" class="imgedit" /><img src="../images/trash.png" class="imgdelete" /></td>                 <td>                     <input type="hidden" value="@item.userid" class="hdnuserid"/></td>             </tr>                     }      </tbody> </table> 

you haven't shown code view, go off of have shown.

first, recommend separating domain model view model. creates separation of concerns, allows modify view model without affecting domain model (and vice versa), , allows send properties necessary view.

create view model this:

public class editableuserdetailsviewmodel {     public bool isadmin { get; set; }     // include other properties editableuserdetails, needed view consume } 

then in controller:

 public actionresult editableuserdetails()     {         viewbag.username = "welcome" + ":" + " " + session["username"].tostring();         viewbag.usertypeid = session["usertypeid"].tostring();          editableuserdetails domainmodel = (from db in entityobj.users                     db.isactive == true                     select new editableuserdetails                     {                         userid = db.userid,                         username = db.username,                         password = db.password,                         category = db.category                     }).tolist();          editableuserdetailsviewmodel viewmodel = new editableuserdetailsviewmodel {             isadmin = ..., // set boolean value based on user's role             // fill in other properties needed view domain model above         };          return view(viewmodel);     } 

(i question why storing welcome message in session, outside scope of question , answer.)

and in view, show admin-only parts if isadmin true:

@if (model.isadmin) {     <div>your person here</div> } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -