首页 > 解决方案 > 为什么 Id 列没有出现在 MVC5 的 CSHTML 视图中

问题描述

我正在研究 MVC5 项目-代码首先,在我的模型中,第一个方法是 Id 方法(键),当我创建视图(列表、创建、详细信息和删除模板)时,id 方法没有标题单元格,但我的模型中的所有其他方法都有。

这是我的模型:

    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [Required(ErrorMessage = "First Name is required ")]
        public string First_Name { get; set; }
        [Required(ErrorMessage = "Last Name is required ")]
        public string Last_Name { get; set; }
        public string Birth_Date { get; set; }
        [Required(ErrorMessage = "Room Number is required ")]
        public int Room_Number { get; set; }
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime Check_In_Date { get; set; }
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime Check_Out_Date { get; set; }
        [Required(ErrorMessage = "Number Of Adults required ")]
        public int Number_Of_Adults { get; set; }
        public Decimal Advanced_Payment { get; set; }

    } 

我的控制器是:

 public ActionResult Index()
        {
            return View(db.controlPanels.ToList());
        }



        [HttpGet]
        public ActionResult Create()
        {
            return View(new ControlPanel());
        }


        [HttpPost]
        public ActionResult Create(ControlPanel controlPanel)
        {
            if (ModelState.IsValid)
            {
                db.controlPanels.Add(controlPanel);
                db.SaveChanges();
                return RedirectToAction("Index");

            }
            else
            {
                return View(controlPanel);

            }
        }

我的观点是:

<table class="table">
    <tr>

        <th>
            @Html.DisplayNameFor(model => model.First_Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Last_Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Birth_Date)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Room_Number)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Check_In_Date)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Check_Out_Date)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Number_Of_Adults)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Advanced_Payment)
        </th>
        <th></th>
    </tr>

当我创建视图时,我需要 Id 列自动出现,就像其他列一样。你能告诉我正确的方法吗?

标签: asp.net-mvcentity-framework

解决方案


推荐阅读