c# - Adding a controller to edit profiles -
i writing small intranet page. displays employees , basic details in list using entity framework interact db , create "employee" class. adding logic allow users edit profiles. current problems :
- the forms work when navigated localhost; using pc-name/webaddress results in 404. causing ? how can fix ?
- the forms empty; want edit user profile id=1 example, of input boxes displayed, empty need type in of information again. can add functionality?
- access not restricted admins , owner of profile (where your
current domain login == "domainac" column in database of users should permitted edit account , not otherwise). whats best solution here?
any in solving of these issues appreciated.
here current setup/flow of information :
displaying users using html/razor : (index.cshtml)
<table id="employeetable"> <thead> <tr> <th>name</th> <th>branch</th> <th>phone no.</th> <th>extension</th> <th>email</th> </tr> </thead> <tbody> @foreach (var prod in model) { <tr> <td>@prod.fullname</td> <td>@prod.branch</td> <td class="js-phonenumbers">@prod.phoneno</td> <td>@prod.extension</td> <td>@prod.email</td> @if (user.isinrole(@"admins") || user.identity.name == prod.domainac) { <td><a href="/home/edit/@prod.id" style="color: blue;">edit</a></td> } else { <td>user => @user.tostring()</td> } <td> <input type="checkbox" name="message" value="@prod.phoneno">message<br> </td> </tr> } </tbody> </table>
homecontroller.cs :
namespace app1.controllers { public class homecontroller : controller { protected edmxdatabase entities = new edmxdatabase(); protected override void dispose(bool disposing) { entities.dispose(); //what do? made default base.dispose(disposing); } public actionresult index() { var products = p in entities.employees orderby p.branch descending select p; return view(products); } [httpget] public actionresult edit(int id = -1) { var employee= new employee(); if (employee == null) { return httpnotfound(); } return view(employee); } } }
edit.cshtml :
@model app1.models.employee @{ viewbag.title = "edit"; } @using (html.beginform()) { <fieldset> <legend>employee</legend> <div class="editor-label"> @html.labelfor(model => model.fullname) </div> <div class="editor-field"> @html.textboxfor(model => model.fullname) </div> <br/> <div class="editor-label"> @html.labelfor(model => model.branch) </div> <div class="editor-field"> @html.textboxfor( model => model.branch ) </div> <br/> <div class="editor-label"> <div style="float: left;"> @html.labelfor(model => model.domainac) </div> @html.textboxfor(model => model.domainac) </div> <br/> <div class="editor-label"> @html.labelfor(model => model.phoneno) </div> <div class="editor-field"> @html.textboxfor( model => model.phoneno) </div> <br/> <input type="submit" value="edit employee details" /> </fieldset> }
the forms work when navigated localhost;
use url helper in index.chtml:
href="@url.action("edit", "home", new { id = prod.id })"
the forms empty;
you should fill model in controller:
[httpget] public actionresult edit(int id = -1) { var employee = new employee(); // here should user data editing instead of creating empty model if (employee == null) { return httpnotfound(); } return view(employee); }
access not restricted admins , owner of profile
the best solution case implement custom autorization attribute crud actions. read more here , after here.
Comments
Post a Comment