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
    {
        [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();
            }


           
        }

             
              
    }
}



please help
Posted

Try changing your Required annotation to Required(AllowEmptyStrings=false) to take care of situations where the string is an empty string.
 
Share this answer
 
Comments
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