Click here to Skip to main content
15,922,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the required validation not working in mvc,After creating the view ,when i click on the submit button,no response from the form

What I have tried:

using System.ComponentModel.DataAnnotations;

namespace Trial.Models
    public class Product
        public int ProdID { get; set; }
        public string ProdName { get; set; }
        public int VendorID { get; set; }

@model Trial.Models.Product
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
    Layout = null;

<!DOCTYPE html>

    <meta name="viewport" content="width=device-width" />

<h3>Are you sure you want to delete this?</h3>
    <hr />
    <dl class="row">
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.ProdID)
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.ProdID)
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.ProdName)
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.ProdName)
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.VendorID)
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.VendorID)

    <form asp-action="Create">
        <input type="submit" value="Delete" class="btn btn-danger" /> |
        <a asp-action="Index">Back to List</a>

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())



    name: "default",
    pattern: "{controller=Prod}/{action=Index}/{id?}");

using Microsoft.AspNetCore.Mvc;
using Trial.Models;

namespace Trial.Controllers
    public class ProdController : Controller
        public IActionResult Index()
            return View();
        public IActionResult Create()
            if (ModelState.IsValid)
                // do something
                return RedirectToPage("index");
                // return RedirectToAction("Index");
                return View();



please help

Try changing your Required annotation to Required(AllowEmptyStrings=false) to take care of situations where the string is an empty string.
Share this answer
Member 11307750 25-Apr-24 6:42am    
It did not work
Validation applies to inputs within a form. You do not have any inputs within your form; instead, you are using Html.DisplayFor to display the values outside of the form.

It's also very odd that you have a button that says Delete posting an empty form to an action called Create.
Share this answer

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