Home » c# » c# – How to view the user activity log in list in ASP.NET Core MVC?-Exceptionshub

c# – How to view the user activity log in list in ASP.NET Core MVC?-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I need to view the users who made updates to their data in a list in a button click (like activity log).

I am using list instead of database for CRUD functions. so when a user make changes in his session, the admin should see what changes he/she made.

So that I have added a new list in which all the edited and approved users copy is saved there.

But the list will update the whole data when ever the user modified it. It all shows the same data.

Mylist in my model class is

profile = new List<Customers>()
            { 
            };

The update function in my model is

public Customers Save(Customers customers)
{
    var chkemp = Customerss.SingleOrDefault(r => r.Id == customers.Id);

    if (chkemp != null)
    {
        chkemp.Fname = customers.Fname;
        chkemp.Lname = customers.Lname;
        chkemp.Location = customers.Location;
        chkemp.Contact = customers.Contact;
        chkemp.Email = customers.Email;
        chkemp.Password = customers.Password;
        chkemp.Category = customers.Category;
        chkemp.Status = customers.Status;
    }

    profile.Add(chkemp);
    return chkemp;
}

This is my view

@{List<Customer.Models.Customers> cust = ViewBag.Customers;

}
<html>
<head>
    <center><h1 style="color:red">User Activity log</h1></center>
</head>
<body>
    <form asp-controller="Home">
        <table class="table">
            <thead>
                <tr>
                    <th>
                        ID
                    </th>
                    <th>
                        FNAME
                    </th>
                    <th>
                        LNAME
                    </th>
                    <th>
                        LOCATION
                    </th>
                    <th>
                        CONTACT
                    </th>
                    <th>
                        EMAIL
                    </th>

                    <th>
                        PASSWORD
                    </th>

                    <th>
                        CATEGORY
                    </th>
                    <th>
                        STATUS
                    </th>

                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach (var cust1 in ViewBag.Customers)
                {
                    <tr>

                        <td>@cust1.Id</td>
                        <td>@cust1.Fname</td>
                        <td>@cust1.Lname</td>
                        <td>@cust1.Location</td>
                        <td>@cust1.Contact</td>
                        <td>@cust1.Email</td>
                        <td>**********</td>
                        <td>@cust1.Category</td>
                        <td>@cust1.Status</td>
                    </tr>
                }
                @*<tr>
                    <td><button type="button" class="btn btn-primary" onclick="location.href='@Url.Action("Details", "Home", new { id})'">Back</button></td>
                </tr>*@



            </tbody>
        </table>
    </form>
</body>
</html>

This is my home controller

public async Task<IActionResult>ProAsync()
{
    HttpResponseMessage Res = await client.GetAsync("https://localhost:44397/api/Values/Pro");

    if (Res.IsSuccessStatusCode)
    {
        List<Customers> user = JsonConvert.DeserializeObject<List<Customers>>(await Res.Content.ReadAsStringAsync());
        ViewBag.Customers = user;
        return View(user);
    }
    else
    {
        return RedirectToAction("Error", "Home");
    }
}

And finally this is my api controller

[HttpGet("Pro")]
public IActionResult Pro(Customers customers)
{
    List<Customers> cust3 = new List<Customers>();
    cust3 = _customerRepo.pro(customers);

    if (cust3 != null)
    {
        return Ok(cust3);
    }
    else
    {
        return (null);
    }
}
How to&Answers: