0

I am trying to concatenate strings firstname and lastname in my model. When I run my project I get an error that reads....*

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

Here is the line of code that is giving me the error.

<input asp-for="@(Model.ContactPersonName + "" + @Model.ContactPersonSurname)" class="form-control" />
Yiyi You
  • 16,875
  • 1
  • 10
  • 22
Zidane
  • 1,696
  • 3
  • 21
  • 35
  • 1
    I think you'd need to create a FullName property on your model that does the concatenation because `for` expects a single field/property... See https://stackoverflow.com/questions/21754071/templates-can-be-used-only-with-field-access-property-access-single-dimension – juharr May 06 '21 at 11:47

1 Answers1

1

If you want to set value with @(Model.ContactPersonName + "" + @Model.ContactPersonSurname),you can use value=....asp-for extracts the name of the specified model property into the rendered HTML. And it can only extract one model property.

<input value="@(Model.ContactPersonName + "" + @Model.ContactPersonSurname)" class="form-control" />
Yiyi You
  • 16,875
  • 1
  • 10
  • 22