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

C# 添加控制器以编辑配置文件,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我目前正在写一个小型的内部网页。它使用entity framework在列表中显示员工及其基本详细信息,以与my DB交互并创建“Employee”类。我目前正在添加逻辑,以允许用户编辑他们的配置文件。我目前的问题是: 表单仅在从localhost导航到时有效;使用 PC-NAME/WebAddress导致404。这可能是什么原因造成的?我怎样才能解决这个问题 表格是空的;我想编辑一个配置文件id为1的用户 例如,将显示所有输入框,但它们都是空的 所以你需要再次输入他们所有的信息。如何添加此功

我目前正在写一个小型的内部网页。它使用entity framework在列表中显示员工及其基本详细信息,以与my DB交互并创建“Employee”类。我目前正在添加逻辑,以允许用户编辑他们的配置文件。我目前的问题是:

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

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

使用HTML/Razor显示用户:(index.cshtml)

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>
}
@model App1.Models.Employee
@{
ViewBag.Title=“编辑”;
}
@使用(Html.BeginForm())
{
雇员
@LabelFor(model=>model.FullName)
@Html.TextBoxFor(model=>model.FullName)

@LabelFor(model=>model.Branch) @Html.TextBoxFor(model=>model.Branch)
@LabelFor(model=>model.DomainAC) @Html.TextBoxFor(model=>model.DomainAC)
@LabelFor(model=>model.PhoneNo) @Html.TextBoxFor(model=>model.PhoneNo)
}
表单仅在从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操作实现自定义自动化属性。阅读更多内容。

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

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>

表单仅在从localhost导航到时有效
在index.chtml中使用url帮助程序:
href=“@url.Action(“编辑”,“主页”,新建{id=prod.id})”
@YD1m这是一个错误!非常感谢!:)2个问题解决了!再次感谢:)如果用户是管理员或者他们当前的域登录名等于他们试图编辑的ID在数据库中的DomainAC值,我如何才能授权?
[Authorize(Roles = "Admin")]
public ActionResult Edit(int id = -1)
 @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();
            }
        }