[英]ASP.NET Core -> Label asp-for not returning label text
出於某種原因,我無法讓我的 label 渲染它只顯示文本框。 我是編程和 .NET 核心的新手。 我認為這是我的一個簡單的錯誤。 我只是看不到它。 請問有什么想法嗎?
Model
namespace Web.Models
{
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public ItemType ItemType { get; set; }
public string MapCode { get; set; }
}
}
Controller
public ViewResult Create()
{
return View();
}
看法
@model Web.Models.Item
<div class="container">
<form asp-action="NewRecord" method="post" class="form-group" role="form">
<div class="form-group row">
<label asp-for="Item.Name"></label>
<div class="col-md-5">
<input asp-for="Item.Name" class="form-control" placeholder="Name" />
</div>
</div>
</form>
</div>
在您看來,如果您明確設置 model,那么您只需輸入該 model 中的屬性名稱。 這意味着您的視圖應類似於以下代碼,並且您不需要並且不能顯式放置Item.Name :
@model Web.Models.Item
<div class="container">
<form asp-action="NewRecord" method="post" class="form-group" role="form">
<div class="form-group row">
<label asp-for="Name"></label>
<div class="col-md-5">
<input asp-for="Name" class="form-control" placeholder="Name" />
</div>
</div>
</form>
另一方面,如果你想使用一些自定義名稱,你應該使用注釋:
using System.ComponentModel.DataAnnotations;
但是,可能在這里您想直接使用視圖模型而不是模型,然后您需要一些 UI 驗證、自定義名稱等。按照這個示例並嘗試理解邏輯。
public class Item
{
public int Id { get; set; }
[Display(Name = "Some name")]
public string Name { get; set; }
[DisplayName("Map code")]
[MaxLength(30)]
public string MapCode { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.