【问题标题】:MVC Readonly Display For ValueMVC 只读显示值
【发布时间】:2018-11-26 09:20:51
【问题描述】:

我有一个 MVC 表单,我想在其中显示一些不可编辑的值。

<form asp-action="AuthorizeTransaction" method="post">
    <div asp-validation-summary="All" class="text-danger"></div>
    <div class="form-group">
        <label asp-for="To"></label>
        <input asp-for="To" class="form-control" />
        <label asp-for="Amount"></label>
        <input asp-for="Amount" class="form-control" />
        <label asp-for="Currency"></label>
        <input asp-for="Currency" class="form-control" />
        <label asp-for="Description"></label>
        <input asp-for="Description" class="form-control" />
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

我知道我可能可以将输入设为只读,但我想知道是否有一种方法可以在不使用文本框的情况下显示表单值并且仍然发布值。我想做这样的事情:

<label asp-for="Amount"></label>
<display asp-for"Amount"></display>
<input type="hidden" asp-for="Amount" class="form-control" />

【问题讨论】:

  • &lt;display asp-for"Amount"&gt;&lt;/display&gt; 在 .NET Core 中是未知的。另请注意,@Html.DisplayFor() helper 只显示值,您需要隐藏字段来回发,以便您可以尝试同时呈现值和隐藏字段的自定义标签助手。

标签: c# html model-view-controller .net-core


【解决方案1】:

据我了解,您希望将某些值显示为不可编辑。因此,您可以使用 readonly 之类的属性,也可以使用 @Html.DisplayFor(m => m.Property) 来自 mvc 的 html 助手,用于使某些内容只读。它将呈现为表单中的标签

【讨论】:

  • 如果我使用 @Html.DisplayFor(m =&gt; m.Property) 这会自动发送到我的表单中还是我仍然需要使用隐藏属性来支持它
  • 是的。您需要隐藏字段将数据从 ui 发送到 Controller
猜你喜欢
  • 2015-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
相关资源
最近更新 更多