简体   繁体   English

混合 C# 和 HTML Helper 标签 ASP.NET MVC 6 (ASP.NET Core)

[英]Mixing C# and HTML Helper tags ASP.NET MVC 6 (ASP.NET Core)

I'm using the new Helper tags in ASP.NET MVC 6.我在 ASP.NET MVC 6 中使用新的 Helper 标签。

    <form asp-area="DAS"
          asp-controller="Report"
          asp-action="Add"
          asp-route-id="@Model.id"
          asp-route-incBalance="@Model.incBalance"
          asp-route-dateSet="@Model.dataStart.ToString("yyyy-MM-dd")"
          asp-route-dateNext="@Model.dataEnd.ToString("yyyy-MM-dd")"
          method="post" role="form">
    </form>

I want to routing attribute:我想路由属性:

asp-route-dateNext="@Model.dataEnd.ToString("yyyy-MM-dd")"

was applied only if:仅适用于:

          {
              if (Model.incBalance == 0)
              {
                  asp-route-dateNext="@Model.dataEnd.ToString("yyyy-MM-dd")"
              }
          }

As a result, I want to get something like this:结果,我想得到这样的东西:

    <form asp-area="DAS"
          asp-controller="Report"
          asp-action="Add"
          asp-route-id="@Model.id"
          asp-route-incBalance="@Model.incBalance"
          asp-route-dateSet="@Model.dataStart.ToString("yyyy-MM-dd")"
          {
             if (Model.incBalance == 0)
             {
                 asp-route-dateNext="@Model.dataEnd.ToString("yyyy-MM-dd")"
             }
          }
          method="post" role="form">
    </form>

I get these errors:我收到这些错误:

TagHelper attributes must be well-formed. TagHelper 属性必须格式良好。

 if (Model.incBalance == 0)

and

The tag helper 'form' must not have C# in the element's attribute declaration area.标签助手“表单”在元素的属性声明区域中不得包含 C#。

 asp-route-dateNext="@Model.dataEnd.ToString("yyyy-MM-dd")"

I'm using Visual Studio 2015 Update 1我正在使用 Visual Studio 2015 更新 1

Update 1: I also tried this option:更新 1:我也试过这个选项:

@(Model.incBalance == 0 ? "asp-route-dateNext=" + Model.dataEnd.ToString("yyyy-MM-dd") : string.Empty)

But the error remained:但错误仍然存​​在:

The tag helper 'form' must not have C# in the element's attribute declaration area.标签助手“表单”在元素的属性声明区域中不得包含 C#。

 @(Model.incBalance == 0 ? "asp-route-dateNext=" + Model.dataEnd.ToString("yyyy-MM-dd") : string.Empty)

With Razor, every part of the markup has to be well formed.使用 Razor,标记的每个部分都必须格式良好。 You cannot have dangling opening tags, or interrupt markup for Razor expresions.您不能有悬空的开始标签,也不能中断 Razor 表达式的标记。 So things like this are not valid and as such cause syntax errors:所以这样的事情是无效的,因此会导致语法错误:

<!-- Interrupting a tag -->
<div
@if (condition) {
     attribute="bar"
}
>

<!-- Also not possible: Conditionally opening tags -->
if (condition) {
    <span>
}
Some text
if (condition) {
    </span>
}

So you have to make sure that the markup within Razor expressions, basically everything between the curly braces, is a valid expression, and that tags are always complete.所以你必须确保 Razor 表达式中的标记,基本上是大括号之间的所有内容,都是一个有效的表达式,并且标签总是完整的。

This unfortunately means that you cannot use @if to conditionally add an attribute (regardless of whether that's a tag helper attribute or not).不幸的是,这意味着您不能使用@if有条件地添加属性(无论它是否是标签助手属性)。

If it was just plain HTML, you could make Razor render raw text, which is not interpreted by the Razor parser and as such does not need to be valid markup (so above things would work).如果它只是普通的 HTML,你可以让 Razor 呈现原始文本,它不会被 Razor 解析器解释,因此不需要是有效的标记(所以上面的东西会起作用)。 However, in raw text, tag helpers also don't run, so that doesn't help you here.但是,在原始文本中,标签助手也不会运行,因此在这里对您没有帮助。

This leaves you essentially with three choices now:这让您现在基本上有三个选择:

  1. Wrap everything in a giant @if and essentially duplicate the whole form tag:将所有内容包裹在一个巨大的@if并基本上复制整个form标签:

     @if (condition) { <form with-that-extra="attribute"> All the form content </form> } else { <form> All the form content </form> }

    Of course, this is just terrible since you need to duplicate really the whole form content (otherwise, the tags within the @if wouldn't be well formed).当然,这很糟糕,因为您需要真正复制整个表单内容(否则, @if的标签将不会形成良好的格式)。

  2. Writing your own tag helper which encapsulates the logic behind the @if condition check.编写您自己的标签助手,它封装了@if条件检查背后的逻辑。

    This works well for more general things, but is a bit tedious just for adding an optional attribute.这适用于更一般的事情,但仅添加可选属性就有点乏味。

  3. Using an expression syntax to pass some “null value” to the route if the condition is not met:如果不满足条件,则使用表达式语法将一些“空值”传递给路由:

     <form … asp-route-dateNext="@(Model.incBalance == 0 ? Model.dataEnd.ToString("yyyy-MM-dd") : null)"> </form>

    This is likely the easiest solution.这可能是最简单的解决方案。 The default value null there will also prevent the tag helper from running for that attribute, so it's just as if you omitted it completely.那里的默认值null也会阻止标签助手为该属性运行,所以就好像你完全省略了它。

I am migrating to aspnet core 3.0 from mvc now, and got the same error.我现在正在从 mvc 迁移到 aspnet core 3.0,并且遇到了同样的错误。 since I didn't use any tag helper features, so I just simply remove the taghelper import directive by comment the line in _viewimports by:因为我没有使用任何标签助手功能,所以我只是简单地通过注释 _viewimports 中的行来删除 taghelper 导入指令:

@*@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers*@

now the views rendered happily.现在视图渲染得很愉快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM