Changing class (or other attributes) of HTML element in server side code

Last Reply 13 days ago By dharmendr

Posted 15 days ago


I am using ASP.NET Core Razor Pages 6.0.

In a .cshtml file, I have a simple element like this:

<div class="mb-3 mt-3">
     <label asp-for="User.firstname" class="form-label">First Name:</label>
     <input asp-for="User.firstname" class="form-control" placeholder="First Name">

How do I access & maniputalte the above <input> element inside the 'OnGet' or 'OnPost' methods?

I need to do so as I want to add a certain class to that <input> element, or make it readonly (depending on certain conditions in my server code).

In older versions of .NET, this was possible by giving an HTML element an id and write runat="server".

Then, one could access the element in the code behind via its id and manipulate it. How is it done now?

Should I not be able to do the same because of the asp-for tag helper which I used? But how?

Thank you for your help!

You are viewing reply posted by: dharmendr 13 days ago.
Posted 13 days ago
mauian says:
I coud even write class="form-label @Model.myclasscolor" etc and assign @Model.myclasscolor in the OnGet method.

Use this way.