[英]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.使用
div
、 span
等。
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
.在这里使用任何自定义条件,只需让它返回
true
或false
。 readonly="@(!string.IsNullOrEmpty(Model.Name))"
试试这个:
<label class="form-control">@Model.Name</label>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.