Html.HiddenFor() 实际上做了什么?

What does Html.HiddenFor() actually do?

Html.HiddenFor() 当我想在页面上保留一个字段但不希望用户注意到它时使用,比如 Id 字段等,但它是否为 Id 字段初始化一个值?如果 Id 字段已经有一个值,那么它会做什么呢?我目前正在学习 ASP.Net MVC 课程,我正在实施一个既可以编辑又可以创建新客户的控制器。为此,我将一个 viewModel 传递给我的控制器,然后检查此 viewModel 中的 Customer 对象是否具有不等于 0 的 Id,如果它等于 0,那么这是我需要的新客户添加到我的数据库,否则我需要编辑它,因为它实际上存在于我的数据库中。那么在这种情况下,Html.HiddenFor() 是做什么的呢?如果我从我的视图中删除 Html.HiddenFor() 标签,当我编辑客户时,它不会被编辑,而是会创建一个新客户。这是为什么?我正在编辑的客户已经有一个 Id,它是我数据库中的关键,那么他的 Id 为什么在我的控制器中被视为 0,那么 Html.HiddenFor() 到底做了什么?我以前看过关于 HiddenFor 的问题,也看过文档,但仍然无法理解它的实际作用,尤其是当确实存在一个值不是 0 的字段时(Id 字段)

我的客户Class:

public class Customer
    {
        public int Id { get; set; }


        [Required] //means that it wont be nullable
        [StringLength(255)] //max len 255 instead of inf 
        public string Name { get; set; }


        public bool IsSubscribedToNewsletter { get; set; }


        [Display(Name = "Membership Type")]
        public MembershipType MembershipType { get; set; }  //navigation property, allows us to navigate from one type to another


        [Display(Name = "Membership Type")]

        public byte MembershipTypeId { get; set; } //foreign key of membership object for optimization purposes
        

        [Display(Name = "Date of Birth")]
        public DateTime? Birthdate { get; set; }  //nullable 
    }

我的 CustomersController 中的保存方法

 [HttpPost]  //make sure not httpget, if modify data, never let it be httpget
        public ActionResult Save(CustomerFormViewModel viewModel) //or use updatecustomerdto (small class we create with only properties we want to update
        {
            if (viewModel.Customer.Id == 0)
            {
                _context.Customers.Add(viewModel.Customer);
            }
            else
            {
                Customer customerInDb = _context.Customers.Single(c => c.Id == viewModel.Customer.Id); //customer object in db
                                                                                                       //need to update its properties to be like those in viemodelcustomer

                //TryUpdateModel(customerInDb,"",new string[] { "Name", "Id" }); //dont use this approach since security conecerns
                //name id is whitelisted as the only things to be updated
                //OR
                //Mapper.Map(customer, customerInDb)

                customerInDb.Name = viewModel.Customer.Name; 
                customerInDb.Birthdate = viewModel.Customer.Birthdate;
                customerInDb.MembershipTypeId = viewModel.Customer.MembershipTypeId;
                customerInDb.IsSubscribedToNewsletter = viewModel.Customer.IsSubscribedToNewsletter;

            }

            _context.SaveChanges(); //must save changes after creating a change in the database

            return RedirectToAction("Index", "Customers");
        }

我的 CustomerFormView,其中 Html.HiddenFor 元素位于最后一个元素之前的行中

@model VidlyProject.ViewModels.CustomerFormViewModel

@{
    ViewBag.Title = "New";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>New Customer</h2>

@using (Html.BeginForm("Save", "Customers"))
{
    <!--we surround it in using since beginform creates a <form> tag not </form> so we need to close it afterwards-->
    <div class="form-group">
        <!--bootstrap class for a responsive form-->
        @Html.LabelFor(m => m.Customer.Name) <!--label-->
        @Html.TextBoxFor(m => m.Customer.Name, new { @class = "form-control" }) <!--next paramter is an anonymous object, where each element is rendered as an html attribute-->
        @**\@class not class because class is reserved in c#-->**@
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.Customer.Birthdate)
        @*OR <lable for="Birthdate">Date Of Birth</label>*@
                                                    @*format string to format the date *@
        @Html.TextBoxFor(m => m.Customer.Birthdate, "{0: d MMM yyyy}", new { @class = "form-control" })
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.Customer.MembershipTypeId)
        @Html.DropDownListFor(m => m.Customer.MembershipTypeId, 
                              new SelectList(Model.MembershipTypes, @*initialize a drop down list*@
                              "Id", @*Name of property in membershiptype class that holds the value for each item*@
                              "Name"), @*Property that holds the text of each item*@
                              "Please select a membership type", @*message at the beginning of the dropdown*@
                              new { @class = "form-control"})
    </div>

    <div class="checkbox">
        <label>
            @Html.CheckBoxFor(m => m.Customer.IsSubscribedToNewsletter, new { @class = "form-check-input" }) Subscribed to the Newsletter?
        </label>
    </div>
    @Html.HiddenFor(c => c.Customer.Id) <!--so that we set an id and the id isnt zero-->
    <button type="submit" class="btn btn-primary">Save</button>
    
}

在这里学习小算法
假设您是酒店的接待员,您的职责是报告进出酒店的顾客。现在在您的酒店,每当有客户进来时,您都会索要他的登记卡。如果客户没有,那么您肯定知道他的新时间到了。在这种情况下,您必须将他注册为新用户。如果他已经有,你就打开他的记录,然后标记时间,然后超时。
现在一位顾客进来声称他已经注册了。您搜索了记录,但他出示的卡是另一张发给不同客户的伪造卡,或者更糟糕的是,该卡上的 ID 不在您的记录中。您别无选择,只能为该用户执行新注册。

这里的问题是,用户没有出示身份证或者身份证是假的。如果您不输入 Html.LabelFor,将没有字段可以保存之前发给用户的 ID