【问题标题】:HtmlHelpers - Html.LabelFor/TextBoxFor/EditorFor in new Dotnet 5HtmlHelpers - 新 Dotnet 5 中的 Html.LabelFor/TextBoxFor/EditorFor
【发布时间】:2021-05-18 03:44:19
【问题描述】:

我有一个从Dotnetcore 3.1 迁移的Dotnet 5.0 MVC 项目,但使用Html.LabelFor, Html.TextBoxFor, Html.EditorFor 的表单不显示输入。

查看:

@model UpdateProductsViewModel;

@{
    using (Html.BeginForm("UpdateProducts", "TheController", FormMethod.Post))
    {
        <p>Browse File:</p>
        Html.LabelFor(m => m.FullName, htmlAttributes: new { @class = "this-doesnt-show"});
        Html.TextBoxFor(m => m.FullName, htmlAttributes: new { @class = "this-neither");
        Html.EditorFor(m => m.FullName, htmlAttributes: new { @class = "this-neither");
        <div>
            <label asp-for="FullName" class="this-shows-with-asp-for"></label>
            <input asp-for="FullName" class="this-shows-with-asp-for" />
        </div>
        <input type='submit' value="Submit" />
    }
}

型号:

public class UpdateProductsViewModel{
  // truncated

  public string FullName { get; set; }
}

在上面的示例中,我有模型和视图。

但是之前我把它改成了asp-for,我把它们改成了Html.LabelFor, Html.EditorFor, Html.TextboxFor这些在 Dotnet 5 中再也没有出现过

当我使用语法&lt;input asp-for=""&gt; | &lt;label asp-for=""&gt; 时,控件显示,换句话说,1x 标签,1x 输入文本框,但其他都没有(HtmlHelper 语法)

  • 即使 Intellisense 检测到 VS Code 中的关键字,这些 Helper 是否不再可用?
  • 我是否遗漏了一些参考资料?
  • 我是否缺少 ViewModel 上的某些属性?
  • 为什么要删除它,因为它们非常有用。

请注意,我不经常将 Dotnet MVC 与 Razor 一起使用,所以可能发生了一些我不知道的变化..

【问题讨论】:

  • 在 Html 助手前面加上 @ 并删除结尾的分号,例如@Html.LabelFor(...)
  • 你是对的,我认为我不需要在每个辅助函数之前使用@,因为初始的@{ }。谢谢!
  • 方法调用之前的@ 确保结果被渲染。 @{ ... } 符号用于打开代码块。
  • 感谢您的澄清。我用这个不足以知道区别。 我认为它只是一个代码块部分,当与助手一起使用时,它并没有真正与渲染相关

标签: c# .net-core razor asp.net-core-razor-pages


【解决方案1】:

您需要使用剃刀语法。像这样的

@model UpdateProductsViewModel;

@{
    using (Html.BeginForm("UpdateProducts", "TheController", FormMethod.Post))
    {
        <p>Browse File:</p>
         @Html.LabelFor(m => m.FullName, htmlAttributes: new { @class = "this-doesnt- 
         show"})
         @Html.TextBoxFor(m => m.FullName, htmlAttributes: new { @class = 
         "thisneither")
          @Html.EditorFor(m => m.FullName, htmlAttributes: new { @class = 
          "thisneither")
        <input type='submit' value="Submit" />
}

}

【讨论】:

  • 正如我提到的,我尝试了 Razor 语法,但没有成功。但是感谢这个例子,每个辅助函数之前缺少的@ 是我的问题。出于某种原因,我认为外部 @ { } 意味着我不需要为辅助函数添加前缀
猜你喜欢
  • 2014-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-14
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多