<textarea> 与"asp-for="一起时不显示内部文本



这在文本区域中显示"Hello there":

@model PlannerViewModel
@{
    ViewData["Title"] = "Test";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Test</h2>
<form method="post">
    <div class="border rounded">
        <textarea class="form-control MyTextArea" rows="4">Hello there</textarea>
    </div>
</form>

这不会在文本区域中显示"你好":

@model PlannerViewModel
@{
    ViewData["Title"] = "Test";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Test</h2>
<form method="post">
    <div class="border rounded">
        <textarea class="form-control MyTextArea" rows="4" asp-for="EditShutdown.Comments_Operations">Hello there</textarea>
    </div>
</form>

如您所见,唯一的区别是"asp-for"的存在。我做错了什么吗?有什么解决方法吗?

要解决这个烦人的事件,您可以使用 ;

@Html.同上

@Html.名称

例;

<textarea id="@Html.IdFor(m => m.EditShutdown.Comments_Operations)" name="@Html.NameFor(m => m.EditShutdown.Comments_Operations)">Hello there</textarea>

asp-for <textarea>提供一个name并填充其值,因此您只需将EditShutdown.Comments_Operations值设置为 "Hello there" 即可显示它。

根据您的要求,您可以尝试使用如下所示placeholder属性:

<form method="post">
<div class="border rounded">
    <textarea class="form-control MyTextArea" rows="4" placeholder="Hello there" asp-for="EditShutdown.Comments_Operations"></textarea>
</div>
</form>

当您执行"创建"操作时,您可以看到提示消息"Hello there";当您执行"编辑"操作时,您可以看到EditShutdown.Comments_Operations的值。

对于asp-for属性,您可以参考:https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro?view=aspnetcore-2.1

最新更新