C# ASP.NET核心MVC和EF核心1.1

C# ASP.NET核心MVC和EF核心1.1,c#,asp.net,asp.net-mvc,entity-framework,asp.net-core,C#,Asp.net,Asp.net Mvc,Entity Framework,Asp.net Core,我对如何添加参数有疑问,请参见下面的代码: [HttpPost] [ValidateAntiForgeryToken] public IActionResult Edit(Employee emp) { try { Employee updateEmp = _Context.Employee.FirstOrDefault(c => c.Idemployee == emp.Idemployee);

我对如何添加参数有疑问,请参见下面的代码:

[HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Edit(Employee emp)
    {
        try
        {

            Employee updateEmp = _Context.Employee.FirstOrDefault(c => c.Idemployee == emp.Idemployee);
            updateEmp.Address = emp.Address;
            updateEmp.Age = emp.Age;
            updateEmp.ContactNumber = emp.ContactNumber;
            updateEmp.FullName = emp.FullName;
            updateEmp.Gender = emp.Gender;
            updateEmp.IsActive = emp.IsActive;

            _Context.Employee.Update(updateEmp);
            _Context.SaveChanges();

            return RedirectToAction("Index");
        }
        catch (Exception)
        {

            throw;
        }
    }
我在这里试图做的是,我试图从querystring中提取emp.idemploye,但它无法将我重定向到error null异常

我正在使用一个模型从cshtml中提取细节

public IActionResult Edit(int id)
    {
        var editEmployee = _Context.Employee.FirstOrDefault(c => c.Idemployee == id);

        return View(editEmployee);
    }
我不知道有什么问题,但是除了IdeEmployee之外,其他所有的信息都被提取出来了

@model Demo101.DAL.Entities.Models.Employee
@{
ViewBag.Title=“编辑员工”;
}
@ViewData[“标题”]
@LabelFor(model=>model.Idemployee,新的{@class=“formcontrol”})
@Html.ValidationMessageFor(model=>model.Idemployee)
@Html.TextBoxFor(model=>model.FullName,新的{@class=“form control”})
@Html.ValidationMessageFor(model=>model.FullName)
@Html.TextBoxFor(model=>model.Age,new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Age)
@Html.TextBoxFor(model=>model.ContactNumber,新的{@class=“form control”})
@Html.ValidationMessageFor(model=>model.ContactNumber)
@Html.TextBoxFor(model=>model.Address,新的{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Address)
@Html.TextBoxFor(model=>model.Gender,新的{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Gender)
@CheckBoxFor(model=>model.IsActive,新的{@class=“form control”})
@Html.ValidationMessageFor(model=>model.IsActive)

您的视图不包含任何具有
Employee
值的字段。您有标签,有验证消息,但没有值本身。因此,您在POST to server中没有任何价值

在表单中添加以下内容:

@Html.HiddenFor(model => model.Idemployee)

您的视图不包含任何具有
Employee
值的字段。您有标签,有验证消息,但没有值本身。因此,您在POST to server中没有任何价值

在表单中添加以下内容:

@Html.HiddenFor(model => model.Idemployee)

看看我下面的代码,试试看

<!--IdEmployee-->
<label asp-for="Idemployee" class="col-md-2 control-label"></label>
<div class="col-md-10">
    @Html.LabelFor(model => model.Idemployee, new { @class = "form-control" })
    @Html.HiddenFor(model => model.Idemployee)
    @Html.ValidationMessageFor(model => model.Idemployee)
</div>

@LabelFor(model=>model.Idemployee,新的{@class=“formcontrol”})
@Html.HiddenFor(model=>model.Idemployee)
@Html.ValidationMessageFor(model=>model.Idemployee)

看看下面我的代码,试试看

<!--IdEmployee-->
<label asp-for="Idemployee" class="col-md-2 control-label"></label>
<div class="col-md-10">
    @Html.LabelFor(model => model.Idemployee, new { @class = "form-control" })
    @Html.HiddenFor(model => model.Idemployee)
    @Html.ValidationMessageFor(model => model.Idemployee)
</div>

@LabelFor(model=>model.Idemployee,新的{@class=“formcontrol”})
@Html.HiddenFor(model=>model.Idemployee)
@Html.ValidationMessageFor(model=>model.Idemployee)

您是否使用CSHTML页面上的员工模型?如果你也发布了cshtml代码,那会有帮助。嗨,@JasonH,是的,我正在cshtml上使用它,我会马上更新我的帖子……你在cshtml页面上使用员工模型吗?如果你也发布cshtml代码的话会有帮助的。嗨,@JasonH,是的,我正在我的cshtml上使用它,我会马上更新我的帖子。。。