Asp.net mvc 为什么Id列不出现在MVC5的CSHTML视图中
我正在首先处理一个MVC5项目代码,在我的模型中,第一个方法是Id方法(键),当我创建视图(列表、创建、详细信息和删除模板)时,Id方法没有标题单元格,但我模型中的所有其他方法都有 这是我的模型:Asp.net mvc 为什么Id列不出现在MVC5的CSHTML视图中,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,我正在首先处理一个MVC5项目代码,在我的模型中,第一个方法是Id方法(键),当我创建视图(列表、创建、详细信息和删除模板)时,Id方法没有标题单元格,但我模型中的所有其他方法都有 这是我的模型: { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Required(ErrorMessage =
{
[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>
@DisplayNameFor(model=>model.First\u Name)
@DisplayNameFor(model=>model.Last_Name)
@Html.DisplayNameFor(model=>model.Birth\u Date)
@Html.DisplayNameFor(model=>model.Room\u编号)
@DisplayNameFor(model=>model.Check\u-In\u-Date)
@DisplayNameFor(model=>model.Check\u Out\u Date)
@Html.DisplayNameFor(model=>model.Number\u成人)
@Html.DisplayNameFor(model=>model.Advanced\u Payment)
我需要Id列在创建视图时自动显示,就像其他视图一样。
您能告诉我正确的方法吗?请同时发布您的视图。我已将其包括在内。原因是id是敏感数据,因此默认情况下不会将其添加到您的视图中。如果你想显示它,你需要手动管理它。谢谢你的回复,我会尝试手动添加它。它按照你说的那样工作,非常感谢