Dario
Dario

Reputation: 3631

Input tag helper isn't allowing the usage of nameof()

I have the following ViewModel in my ASP.NET Core 1.1 application:

public class HomepageViewModel
{
    [Required]
    public string Description{ get; set; }
}

The ViewModel mentioned above is used in a view with an input field like the one below. As you can see, I'm trying to specify the asp-for property of the input tag helper by using nameof(), so that in case I rename the property in the ViewModel, the changes will also be applied to the view.

@model HomepageViewModel
[... various HTML Code...]

<input asp-for="@nameof(Model.Description)" class="form-control" />

In any case this isn't working since I receive the following error message:

System.InvalidOperationException - Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

Is there a way to use nameof() as I'm trying to do, or isn't it allowed for any specific reasons?

Upvotes: 1

Views: 197

Answers (1)

Daniel A. White
Daniel A. White

Reputation: 190897

The asp-for is enough for a reference, a smart enough tool should detect those and rename appropriately.

For instance, I was able to rename a property using Resharper and it changed my view appropriately.

Upvotes: 1

Related Questions