c# - Adding functions to a webpage where visibility is restricted by your domain login/role -
i working on small, simple project. have list of users stored in database so: id (uniqueidentifier primary key), firstname(varchar), lastname(varchar), phoneno(varchar), domainac(varchar)
i displaying information on webpage :
<%@ page language="c#" inherits="system.web.mvc.viewpage<ienumerable<whois.models.employee>>" %> <!doctype html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>index</title> </head> <body> <p> <%: html.actionlink("create new", "create") %> </p> <table> <tr> <th> <%: html.displaynamefor(model => model.fullname) %> </th> <th> <%: html.displaynamefor(model => model.phoneno) %> </th> <th> <%: html.displaynamefor(model => model.domainac) %> </th> <th> <%: html.displaynamefor(model => model.branch) %> </th> <th></th> </tr> <% foreach (var item in model) { %> <tr> <td> <%: html.displayfor(modelitem => item.fullname) %> </td> <td> <%: html.displayfor(modelitem => item.phoneno) %> </td> <td> <%: html.displayfor(modelitem => item.domainac) %> </td> <td> <%: html.displayfor(modelitem => item.branch) %> </td> <td> <% if (user.isinrole(@"admin") || user.identity.name == item.domainac) {%> <%: html.actionlink("edit", "edit", new { id=item.id }) %> | <%: html.actionlink("delete", "delete", new { id=item.id }) %> <% } %> </td> </tr> <% } %> </table> </body> </html>
this works ok, throws little webpage of users , has 3 pointless links displayed alongside them - i'm not sure go here - how make these links work simply/effectively possible ?
the functionality
if(domainac == logged in domain ac || logged in domain ac admin) allow user edit/update details. else { user normal, no buttons/actions - display list. }
can explain me need make work please ? posted question similar before, , has went on head - talk of actioncontrollers etc, no explanation of go in project/how link together/how used.
can me please? putting small bounty on in 2 days , awarding best answer that's cropped lot me lately! thank you.
well answer isnt horribly complicated, depends on how want implement it. (i assuming you're using mvc due how of implemented , how have tagged)
in controller need determine if user logged in , data. ok, lets have that
public actionresult loaduserdata(){ var model = new loaduserdatamodel(); // code here populate model var isuserauthenticated = methodtofigureoutifuserisauthenticated(); // returns bool viewbag.isuserauthenticated = isuserauthenticated; // don't using viewbag can add model if wish. return view(model); }
not gonna lie, experts out there looking @ might freaking out bit. understand. method attribute far more effective i'm trying fellow out here.
ok on view (i copied yours , made few tweaks)
<%@ page language="c#" inherits="system.web.mvc.viewpage<ienumerable<whois.models.employee>>" %> <!doctype html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>index</title> </head> <body> <% if (viewbag.isuserauthenticated) {%> <p> <%: html.actionlink("create new", "create") %> </p> <% } %> <table> <tr> <th> <%: html.displaynamefor(model => model.fullname) %> </th> <th> <%: html.displaynamefor(model => model.phoneno) %> </th> <th> <%: html.displaynamefor(model => model.domainac) %> </th> <th> <%: html.displaynamefor(model => model.branch) %> </th> <th></th> </tr> <% foreach (var item in model) { %> <tr> <td> <%: html.displayfor(modelitem => item.fullname) %> </td> <td> <%: html.displayfor(modelitem => item.phoneno) %> </td> <td> <%: html.displayfor(modelitem => item.domainac) %> </td> <td> <%: html.displayfor(modelitem => item.branch) %> </td> <td> <% if (viewbag.isuserauthenticated) {%> <%: html.actionlink("edit", "edit", new { id=item.id }) %> | <%: html.actionlink("delete", "delete", new { id=item.id }) %> <% } %> </td> </tr> <% } %> </table> </body> </html>
and ... should work you.
so part of question. wanted know how of ties together. answer not easy answer in simple question answer forum. so... give brief overview of roles are.
mvc stands model/view/controller. methodology of programming separates concerns of application development. controller data/getter/grabber/saver of application. there whole level of understanding business application development can't explained in less 4 years @ major university, lets meat , potatoes of application. view presentation of application. known user interface. portion controls applications user input , actual actions of application. model glue ties them together. controller grab data needed fill model, , pass model view, , view consume it.
why necessary? not necessary, absolutely required if want develop applications maintainable , code reusable. thats beauty of mvc.
i hope answered question. if need more clarification let me know , update answer accordingly.
i suggest go website http://www.asp.net/mvc , right, see section says pluralsight , web series on mvc4. found helpful when getting started mvc.
Comments
Post a Comment