简体   繁体   English

ASP.NET 核心 -> Label asp-for 不返回 label 文本

[英]ASP.NET Core -> Label asp-for not returning label text

For some reason I can not get my label to render it just displays the text box.出于某种原因,我无法让我的 label 渲染它只显示文本框。 I am new to programming and .NET Core.我是编程和 .NET 核心的新手。 I reckon this is a simple of mistake of mine.我认为这是我的一个简单的错误。 I just cant see it.我只是看不到它。 Any ideas please?请问有什么想法吗?

Model 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 Controller

public ViewResult Create()
{
    return View();
}

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>

在此处输入图像描述

In your view if you set explicitly model then you only need to put name of property from that model.在您看来,如果您明确设置 model,那么您只需输入该 model 中的属性名称。 That means your view should looks like following code and you don't need and you cannot explicitly put Item.Name :这意味着您的视图应类似于以下代码,并且您不需要并且不能显式放置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>

On other side if you want to use some custom name you should use annotations:另一方面,如果你想使用一些自定义名称,你应该使用注释:

using System.ComponentModel.DataAnnotations;

Hovewer, probably here you want to use View Models instead directly Models then you need some UI validation, custom names etc. Follow this example and try to understand logic.但是,可能在这里您想直接使用视图模型而不是模型,然后您需要一些 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.

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