je veux besoin de creer une application gestion des utilisateurs en MVC et comment creer les action methodes pour faire l addition une nouvelle user et afficher dans grid view
Translated
I want to need to create a user management application in MVC and how to create the action methods to add a new user and display in grid view

Vijay Pratap SinghRank #381 · 6.6K reputationPosted Oct 30, 2024, 6:46 PM EDT
Create a new ASP.NET Core MVC project in Visual Studio.
Create a User model that represents the users in your application. This model will map to a database table.
using System.ComponentModel.DataAnnotations; namespace YourNamespace.Models { public class User { public int Id { get; set; } [Required] [Display(Name = "User Name")] public string Name { get; set; } [Required] [EmailAddress] public string Email { get; set; } [Required] [Display(Name = "Phone Number")] public string PhoneNumber { get; set; } } }Install Entity Framework Core and create a UserContext class.
Add the necessary NuGet packages
a. Microsoft.EntityFrameworkCore.SqlServer
b. Microsoft.EntityFrameworkCore.Tools
In the
Datafolder, create UserContext.cs to represent the database contextusing Microsoft.EntityFrameworkCore; using YourNamespace.Models; namespace YourNamespace.Data { public class UserContext : DbContext { public UserContext(DbContextOptions options) : base(options) { } public DbSet Users { get; set; } } }Configure the database connection in the appsettings.json file.
"ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=UserManagementDB;Trusted_Connection=True;MultipleActiveResultSets=true" }Register the UserContext in the Program.cs file.
builder.Services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));Run the migrations to create the database
dotnet ef migrations add InitialCreate dotnet ef database updateIn the Controllers folder, create UserController.cs with actions to add and display users.
using Microsoft.AspNetCore.Mvc; using YourNamespace.Data; using YourNamespace.Models; using System.Linq; using System.Threading.Tasks; namespace YourNamespace.Controllers { public class UserController : Controller { private readonly UserContext _context; public UserController(UserContext context) { _context = context; } // Action to show all users public async Task Index() { var users = await _context.Users.ToListAsync(); return View(users); } // Action to display form to add a new user public IActionResult Create() { return View(); } // Action to save new user to the database [HttpPost] [ValidateAntiForgeryToken] public async Task Create(User user) { if (ModelState.IsValid) { _context.Add(user); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } return View(user); } } }In the View/User folder, create the views for displaying users (Index.cshtml) and adding a new user (Create.cshtml).
Index.cshtml - Display Users in a Grid View
This view displays the list of users in a simple table layout.
@model IEnumerable @{ ViewData["Title"] = "User List"; } User List Add New User ID Name Email Phone Number @foreach (var user in Model) { @user.Id @user.Name @user.Email @user.PhoneNumber }Create.cshtml - Form to Add a New User
This view provides a form to add new users.
@model YourNamespace.Models.User @{ ViewData["Title"] = "Add New User"; } Add New User Save @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }Run the application.
Hope this will help.
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Oct 31, 2024, 3:02 AM EDT
Hi Samia,
You can walk through this step-by-step article and also download the source code.
https://www.c-sharpcorner.com/article/create-identity-in-simple-ways-using-asp-net-mvc-5/
Translated:
Vous pouvez suivre cet article étape par étape et également télécharger le code source.
ttps://www.c-sharpcorner.com/article/create-identity-in-simple-ways-using-asp-net-mvc-5/
Thank you (Merci)