Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I work on asp.net razor page model . i face issue i can't edit data table on

same page by edit cell without redirect to another page

I need to edit cell on same page


I need to edit data on table by edit every cell on rows

my code details as below :

What I have tried:

ASP.NET
public class UserModel
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    // add other user properties as needed
}

public class IndexModel : PageModel
{
    private readonly YourDbContext _dbContext; // replace with your DbContext class

    public IndexModel(YourDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public List<UserModel> Users { get; set; }

    public void OnPost(string userId)
    {
        Users = _dbContext.Users
            .Where(u => u.Id == userId)
            .Select(u => new UserModel
            {
                Id = u.Id,
                Name = u.Name,
                Email = u.Email
                // map other user properties as needed
            })
            .ToList();
    }
}
@if (Users != null && Users.Count > 0)
{
    <table class="table">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Email</th>
                <!-- add other user property headers as needed -->
            </tr>
        </thead>
        <tbody>
            @foreach (var user in Users)
            {
                <tr>
                    <td>@user.Id</td>
                    <td>@user.Name</td>
                    <td>@user.Email</td>
                   <td>
          <button class="btn btn-primary edit-btn" data-id="@user.Id">Edit</button>
        </td>
                </tr>
            }
        </tbody>
    </table>
}
Posted
Comments
Andre Oosthuizen 10-Jun-23 6:02am    
Your code looks like copied and pasted code from another source and I would recommend that you try and understand that part first. You then need to click on the "Improve Question' above to show us what code you have for editing any cells or rows, your current code is just to populate the data table. At the moment we have no idea where you are stuck regarding editing cells or rows.

There is an article here on CodeProject for editing a DataTable using ASP.NET MVC which might help -
ASP.NET MVC Editable Table (jQuery DataTables and ASP.NET MVC integration[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900