using System.ComponentModel.DataAnnotations; namespace Trial.Models { public class Product { [Key] public int ProdID { get; set; } [Required] public string ProdName { get; set; } public int VendorID { get; set; } } } //////////////////////////////////////////////////// @model Trial.Models.Product @addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Create</title> </head> <body> <h3>Are you sure you want to delete this?</h3> <div> <h4>Product</h4> <hr /> <dl class="row"> <dt class = "col-sm-2"> @Html.DisplayNameFor(model => model.ProdID) </dt> <dd class = "col-sm-10"> @Html.DisplayFor(model => model.ProdID) </dd> <dt class = "col-sm-2"> @Html.DisplayNameFor(model => model.ProdName) </dt> <dd class = "col-sm-10"> @Html.DisplayFor(model => model.ProdName) </dd> <dt class = "col-sm-2"> @Html.DisplayNameFor(model => model.VendorID) </dt> <dd class = "col-sm-10"> @Html.DisplayFor(model => model.VendorID) </dd> </dl> <form asp-action="Create"> <input type="submit" value="Delete" class="btn btn-danger" /> | <a asp-action="Index">Back to List</a> </form> </div> </body> </html> //////////////////////////////////////////////////////////////// var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{controller=Prod}/{action=Index}/{id?}"); app.Run(); ////////////////////////////////////////////////////////////// using Microsoft.AspNetCore.Mvc; using Trial.Models; namespace Trial.Controllers { public class ProdController : Controller { public IActionResult Index() { return View(); } [HttpPost] public IActionResult Create() { if (ModelState.IsValid) { // do something return RedirectToPage("index"); // return RedirectToAction("Index"); } else { return View(); } } } }
Required
Required(AllowEmptyStrings=false)
Html.DisplayFor
Delete
Create
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)