简体   繁体   English

在 ASP.NET Core 中显示只读字段

[英]Display a readonly field in ASP.NET Core

I am new in ASP.NET Core and would like to ask about displaying a field in wa view.我是 ASP.NET Core 的新手,想询问有关在 wa 视图中显示字段的问题。 I have a我有一个

class Person { 
    public string Name {get; set;} 
    public string Nickname {get; set;} 
}

Typically, in the EditPerson View, I do通常,在EditPerson视图中,我会

<input asp-for="Name" class="form-control" />
<input asp-for="Nickname" class="form-control" />

to display the fields.以显示字段。

But in my EditPerson View I need to display Name as readonly, and Nickname as editable field.但在我的EditPerson视图中,我需要将Name显示为只读,并将Nickname为可编辑字段。

The Name should not be in a textbox (even readonly), but rather in a label or div ... Name不应在文本框中(甚至只读),而应在标签div 中...

Is there a standard approach to display such fields?是否有显示此类字段的标准方法?

I don't want that the field to be updated back to the model or DB, I need only reading the field.我不希望该字段更新回模型或数据库,我只需要读取该字段。

Try this.试试这个。

<input asp-for="Name" class="form-control" readonly="@(true)">
<input asp-for="Name" class="form-control" readonly="@(false)">

This render:这个渲染:

<input class="form-control" type="text" id="Name" name="Name" value="Tom" readonly="readonly">
<input class="form-control" type="text" id="Name" name="Name" value="Tom">

I realized it from here: https://github.com/aspnet/Mvc/issues/7333#issuecomment-363504164我是从这里意识到的: https : //github.com/aspnet/Mvc/issues/7333#issuecomment-363504164

Output it as HTML with Razor syntax使用 Razor 语法将其输出为 HTML

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/razorhttps://docs.microsoft.com/en-us/aspnet/core/mvc/views/razor

@Model.Name
<input asp-for="Nickname" class="form-control" />

Note - this will need styling appropriately, or wrapping in <span> tag etc...注意 - 这将需要适当的样式,或包装在<span>标签等...

This worked for me这对我有用

<input asp-for="Name" class="form-control" ReadOnly="true"/>
<input asp-for="Nickname" class="form-control" />
[ReadOnly(true)]
public string Name {get; set;} 

Add a ReadOnly attribute.添加只读属性。 Remember to include using System.Component;记得using System.Component; at the top of your class.在你的班级中名列前茅。

If you want to display the model property Name and don't want it to be an input element, just do this:如果要显示模型属性Name并且不希望它成为input元素,只需执行以下操作:

<p>@Model.Name</p>

Important: if you have used the DisplayFormat attribute on Name in your model (eg to apply number/date formatting or to control how blanks/nulls are displayed), you must do this instead:重要提示:如果您在模型中使用了Name上的DisplayFormat属性(例如应用数字/日期格式或控制空白/空值的显示方式),则必须改为执行以下操作:

<p>@Html.DisplayFor(m => m.Name)</p>

Of course, it doesn't need to be a p .当然,它不需要是p Use a div , span , etc.使用divspan等。

Give it a class and style it with CSS as desired.给它一个类并根据需要使用 CSS 设置样式。

这对我有用:

<input asp-for="Name" class="form-control" readonly="readonly" />

You can try this:你可以试试这个:

<input asp-for="Name" readonly="@(!string.IsNullOrEmpty(Model.Name))" />
<input asp-for="Nickname" />

Using any custom condition here, just make it return true or false .在这里使用任何自定义条件,只需让它返回truefalse readonly="@(!string.IsNullOrEmpty(Model.Name))"

试试这个:

<label class="form-control">@Model.Name</label>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 动态添加类到 ASP.NET 中的只读字段 - Dynamically adding class to readonly field in ASP.NET 如何使ItemTempleField成为ASP.Net中的ReadOnly字段 - How to make ItemTempleField to ReadOnly Field in ASP.Net 如何使用 ASP.NET Core 显示错误 - How to display errors with ASP.NET Core 如何更改asp.net Core Razor页面中的DateTime字段显示精度 - How to change DateTime field display precision in asp.net Core Razor Pages 如何防止在 ASP.NET Core 中的 Razor 视图 HTML 中禁用或只读输入文件的模型绑定? - How to prevent model binding of disabled or readonly input fileds in Razor view HTML in ASP.NET Core? 如何在 ASP.NET Core MVC 中的 Edit 方法的下拉字段中显示 selectedValue(来自数据库) - How can I display the selectedValue (from database) in a dropdown-field from the Edit method in ASP.NET Core MVC asp.net核心2-特定字段的自定义模型绑定 - asp.net core 2 - Custom model binding for specific field 如果值为 True,则在 ASP.Net Core 的创建视图中解锁字段 - If a value is True, unlock a field in the creation view in ASP.Net Core ASP.NET 核心本地化小数域点和逗号 - ASP.NET Core localization decimal field dot and comma 在Asp.net Core中对IFormFile字段进行xunit测试 - xunit test for IFormFile field in Asp.net Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM