C# ASP.NET Core string expression in a taghelper property

121 Views Asked by At

In my tag helper I declared a property like this:

[HtmlTargetElement("tr", TagStructure = TagStructure.NormalOrSelfClosing)]
public class EditTableTRTag : TagHelper
{
    [HtmlAttributeName("edit-rowId")]
    public Object RowId { get; set; }
}

In the process method I check if it is a null or not, then apply the .ToString() to generate a data-row-id="..." attribute from its value:

public override void Process(TagHelperContext context, TagHelperOutput output)
{
    if (RowId != null && IsTotalRow==false) 
        output.Attributes.Add("data-row-id", RowId.ToString());
}

In the .cshtml I have a local Int32 value to help generating incremental row ID expressions.

@{ var i = 0; }
@foreach(....)
{
   <tr edit-rowId="@(i)_th_row" ...
   ...
}

The above code does not seem to work:

Error CS0118: 'rowid' is a variable but it is used like a type
Error CS0103: The name '_th_row' does not exist in the current context

The following code does not throw any compile-time error:

<tr data-row-id="@(i)_th_row" ...

I wonder why it is, and what is the solution?

Note: I declared the property as Object not String for a reason: I want to use as the following (where MyEnum is an enum):

<tr edit-rowId="@MyEnum.MyOption1" 

But: when I changed the property type from Object to String, the compile-time error disappeared. Still don't know how to keep the property type as Object and avoid the compile-time error.

0

There are 0 best solutions below