C# HtmlHelpers-Html.LabelFor/TextBoxFor/EditorFor在新的Dotnet 5中

C# HtmlHelpers-Html.LabelFor/TextBoxFor/EditorFor在新的Dotnet 5中,c#,.net-core,razor,asp.net-core-razor-pages,C#,.net Core,Razor,Asp.net Core Razor Pages,我有一个从Dotnetcore 3.1迁移过来的dotnet5.0MVC项目,但是使用Html.LabelFor、Html.TextBoxFor、Html.EditorFor的表单不显示输入 查看: @model UpdateProductsViewModel; @{ using (Html.BeginForm("UpdateProducts", "TheController", FormMethod.Post)) { &

我有一个从
Dotnetcore 3.1
迁移过来的
dotnet5.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中再也没有出现过

当我使用语法
|
时,控件显示,换句话说,1x标签,1x输入文本框,但没有其他(HtmlHelper语法)

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

请注意,我没有定期将Dotnet MVC与Razor一起使用,因此可能发生了一些我不知道的变化。

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

@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" />
}
@model UpdateProductsViewModel;
@{
使用(Html.BeginForm(“UpdateProducts”,“TheController”,FormMethod.Post))
{
浏览文件:

@LabelFor(m=>m.FullName,htmlAttributes:new{@class=“这不- 显示“}) @TextBoxFor(m=>m.FullName,htmlAttributes:new{@class= “这两个都不是”) @EditorFor(m=>m.FullName,htmlAttributes:new{@class= “这两个都不是”) }

}

在Html帮助程序前面加上
@
前缀,并删除后面的分号,例如
@Html.LabelFor(…)
您是对的,我想我不需要在每个帮助程序函数前面加
@
,因为开头是
@{}
。谢谢!方法调用之前的
@
可确保呈现结果。
@{…}
表示法用于打开代码块。感谢您的澄清。我没有充分使用它来了解差异。我认为它只是一个代码块部分,当与助手一起使用时,它实际上与渲染无关。正如我提到的,我尝试了Razor语法,但它不起作用。但是感谢您的示例,e之前缺少的
@
每个helper函数是我的问题。出于某种原因,我认为外部的
@{}
意味着我不需要为helper函数加前缀