Product.cs
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; }
}
}
ProdController.cs
using Microsoft.AspNetCore.Mvc;
using Trial.Models;
namespace Trial.Controllers
{
public class ProdController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Create(Product Prod)
{
if (ModelState.IsValid)
{
// do something
return RedirectToPage("index");
// return RedirectToAction("Index");
}
else
{
return View();
}
}
}
}
Create.cshtml
@model Trial.Models.Product
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Create</title>
</head>
<body>
<h4>Product</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="ProdID" class="control-label"></label>
<input asp-for="ProdID" class="form-control" />
<span asp-validation-for="ProdID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ProdName" class="control-label"></label>
<input asp-for="ProdName" class="form-control" />
<span asp-validation-for="ProdName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="VendorID" class="control-label"></label>
<input asp-for="VendorID" class="form-control" />
<span asp-validation-for="VendorID" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back To List</a>
</div>
</body>
</html>
Program.cs
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();
PLEASE HELP