添加控制器以编辑配置文件

本文关键字:配置文件 编辑 控制器 添加 | 更新日期: 2023-09-27 18:24:57

我目前正在编写一个小型intranet页面。它使用实体框架在列表中显示员工及其基本细节,以与我的DB交互并创建一个"Employee"类。我目前正在添加逻辑,以允许用户编辑他们的配置文件。我目前的问题:

  • 表单只有在从localhost导航到时才能工作;使用PC-NAME/WebAddress导致404。是什么原因造成的?我该怎么解决这个问题
  • 表格是空的;我想编辑配置文件id为1的用户例如,显示了所有的输入框,但它们都是空的所以你需要再次输入他们的所有信息。如何添加此功能
  • 访问权限不限于管理员和配置文件的所有者(如果您的
    当前域登录==我的用户数据库中的"domainAC"列应允许编辑帐户,而不是以其他方式)。这里最好的解决方案是什么

如果能为解决这些问题提供任何帮助,我们将不胜感激。

以下是我当前的设置/信息流:

使用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 does this do? It was made by default
            base.Dispose(disposing);
        }
        public ActionResult Index()
        {
            var products =
                from 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>
}

添加控制器以编辑配置文件

表单只有在从localhost导航到时才能工作;

在index.chtml:中使用url帮助程序

href="@Url.Action("Edit", "Home", new { id = prod.Id })"

表格是空的;

你应该在控制器中填写你的型号:

        [HttpGet]
        public ActionResult Edit(int id = -1)
        {
            var employee = new Employee(); // here you should to get user data for editing instead of creating empty model
            if (employee == null)
            {
                return HttpNotFound();
            }
            return View(employee);
        }

访问权限不限于管理员和配置文件的所有者

对于这种情况,最好的解决方案是为CRUD操作实现自定义的自动化属性。点击此处阅读更多信息。

对于您的第二个问题,表单为空的原因是您正在传递一个没有数据可显示的Employee的新实例,请尝试进行测试。

var employee= new Employee { PhoneNo = "12345"};

通过对类或方法使用Authorize属性,可以按角色限制访问。

[Authorize(Roles = "Admin")]
public ActionResult Edit(int id = -1)

您的url

 @Html.ActionLink("Edit", "Home", new { RID = m.RID })
            OR
 <a href='Edit/@m.RID ' >m.REName</a>

控制器

 public ActionResult Edit(string RID)
        {
            int _RID = 0;
            int.TryParse(RID, out _RID);
            RegisterModel model = new RegisterModel();
            if (_RID > 0)
            {
               var re = (from r in _context.Registrations where r.RID == _RID select r).First();
               model.RID = _RID;
               model.REName = re.REName;
               model.REAddress = re.REAddress;
               model.REPhoneNo = re.REPhoneNo;
                return View(model);
            }
            else
            {
            return View();
            }
        }