简体   繁体   English

ASP.NET Core MVC和EF Core 1.1

[英]ASP.NET Core MVC and EF Core 1.1

I have question in regarding how to add parameter see my codes below: 我对如何添加参数有疑问,请参见下面的代码:

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

What I am trying to do here is I am trying to extract the emp.Idemployee from the querystring but it couldn't and redirect me to the error null exception. 我在这里想要做的是我正在尝试从查询字符串中提取emp.Idemployee,但它无法将我重定向到错误null异常。

I am using a model to extract the details form the cshtml. 我正在使用一个模型从cshtml提取细节。

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

        return View(editEmployee);
    }

I don't know whats wrong with it but all of the other information are extracted except for Idemployee. 我不知道这有什么问题,但除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> 

Thanks! 谢谢!

Look at my code below, give this a go 看下面的代码,试试看

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

Your view does not contain any field with Employee value. 您的视图不包含任何具有Employee值的字段。 You have label, you have validation message, but not value itself. 您有标签,您有验证消息,但本身没有价值。 Thus, you have no value in POST to server. 因此,在POST到服务器中没有任何价值。

Add somewhere inside form: 在表格内添加:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM