T
T
touppercase2022-04-21 15:09:30
ASP.NET
touppercase, 2022-04-21 15:09:30

How to bind link/button behavior to a specific class instance?

There is such a class:

public class Profile
{
    public static readonly List<Profile> Profiles = new()
    {
        new Profile("Profile #1", "0.0.0.0"),
        new Profile("Profile #2", "0.0.0.0"),
        new Profile("Profile #3", "0.0.0.0"),
        new Profile("Profile #4", "0.0.0.0"),
        new Profile("Profile #5", "0.0.0.0")
    };

    public string ProfileName { get; set; }
    public string IpAddress { get; set; }

    private Profile(string profileName, string ipAddress)
    {
        ProfileName = profileName;
        IpAddress = ipAddress;
    }
}

And there is such a view:
@foreach (var item in Profile.Profiles)
{
    <div>
        <h1>@item.ProfileName</h1>
        <h4>@item.IpAddress</h4>
        <a href="#">Do something</a>
    </div>
}

Here I just loop through the list and display both properties of each instance on the page. Let's say I want something to happen to a specific instance when a link or button is clicked. For example, the color of the profile name changed, or I need to be able to, for example, edit the field with the ip-address. I do not understand how to make friends with a link with a specific instance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nik Faraday, 2022-04-23
@NikFaraday

I didn’t understand the task a little, so I’ll give an answer to what I understood)
First, try using JS if you need to change “Something” depending on “Something”, but this, as you understand, works, if you need changes on the view (Even Vue.js is better here).
Second, try using other classes for such tasks. Here you will already need (Probably) Ajax to work correctly with views. Through Ajax, try to redirect the click "Somewhere" and process it there

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question