Controller: public class CustomerController : Controller { private readonly ApplicationDbContext _dbcontext; public CustomerController(ApplicationDbContext applicationDbContext) { _dbcontext = applicationDbContext; } public IActionResult Index() { return View(_dbcontext.Customers.Select(c=> new CustomerViewModel() { Name=c.CustomerName, Descriptsion=c.Description}).ToList()); } [HttpPost] public IActionResult Create(CustomerViewModel model) { if (ModelState.IsValid) { _dbcontext.Customers.Add(new Customer() { CustomerName = model.Name, Description = model.Descriptsion }); _dbcontext.SaveChanges(); } //after create new item, requery the database and return the CustomerList partial view to update the list. return PartialView("_CustomerListPV", _dbcontext.Customers.Select(c => new CustomerViewModel() { Name = c.CustomerName, Descriptsion = c.Description }).ToList()); } } Index: @model List @{ ViewData["Title"] = "Index"; }
@section Scripts{ } _CustomerListPV @model List

Create New

@if(Model != null && Model.Count > 0){ foreach (var item in Model) { } } else{ }
Name Description
@Html.DisplayFor(modelItem => item.Name) @Html.DisplayFor(modelItem => item.Descriptsion) @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) | @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) | @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
Empty