【问题标题】:ASP.NET Core MVC and EF Core 1.1ASP.NET Core MVC 和 EF Core 1.1
【发布时间】:2017-03-22 07:30:22
【问题描述】:

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

[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;
        }
    }

我在这里尝试做的是尝试从查询字符串中提取 emp.Idemployee,但它无法将我重定向到错误空异常。

我正在使用模型从 cshtml 中提取详细信息。

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

        return View(editEmployee);
    }

我不知道它有什么问题,但除 Idemployee 之外的所有其他信息都已提取。

@model Demo101.DAL.Entities.Models.Employee

@{
    ViewBag.Title = "Edit Employee";
}

<h2>@ViewData["Title"]</h2>

<form asp-controller="Employee" asp-action="Edit" method="post" class="form-horizontal" role="form">
    <div class="form-horizontal">
        <div asp-validation-summary="All" class="text-danger"></div>

        <!--FullName-->
        <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.ValidationMessageFor(model => model.Idemployee)
        </div>

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

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

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

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

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

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

        <!--Create Button-->
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Edit Employee" class="btn btn-default" />
        </div>
    </div>
</form>

谢谢!

【问题讨论】:

  • 您是否在 CSHTML 页面上使用员工模型?如果您也发布 cshtml 代码会有所帮助。
  • 嗨,@JasonH,是的,我在我的 CSHTML 上使用它,我会立即更新我的帖子...

标签: c# asp.net asp.net-mvc entity-framework asp.net-core


【解决方案1】:

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

<!--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>

【讨论】:

    【解决方案2】:

    您的视图不包含任何具有Employee 值的字段。你有标签,你有验证信息,但没有价值本身。因此,您在 POST 到服务器中没有任何价值。

    在表单中的某处添加:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 1970-01-01
      • 2017-04-24
      相关资源
      最近更新 更多