Razor validation not working

App Dev 86 Reputation points
2022-07-01T15:25:44.317+00:00

Hi All I have a page that uses Razor validation and for some reason the page no longer validates.
I have no idea what could have gone wrong.

I am using something called a partial class to house the validation since I don't understand view models all that well.
the partial class has the validation set and this was working... but now a user can submit a page without
entering data in the required fields.

Can some one give me some ideas on what could have gone silly??

Here is a sample of the view the partial calls and the Controller...
I can't imagine what could have cause this to stop working

The form view
@默 DUST.Models.WRITE_REQ_REQUEST_TBL

@{  
    ViewBag.Title = "NWR";  
    //Layout = "~/Views/Shared/_LayoutWRITE.cshtml";  
    Layout = "~/Views/WRITE/SharedViews/_LayoutWRITE.cshtml";  
}  
  
*@  
  
  
  
@using (Html.BeginForm())  
{  
    @Html.AntiForgeryToken()  
  
    <div class="form-horizontal">  
        @*<h4>WRITE_REQ_REQUEST_TBL</h4>*@  
        <hr />  
  
  
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })  
        <div class="form-group">  
            @Html.LabelFor(model => model.REQ_REQUEST_DIV_ID, htmlAttributes: new { @class = "control-label col-md-2" })  
            <div class="col-md-4">  
                @Html.DropDownListFor(model => model.REQ_REQUEST_DIV_ID, new SelectList(ViewBag.DDL_Divisions, "Division_ID", "Division"), "--SELECT ONE--", new { @class = "form-control", @Style = "text-transform: uppercase;" })  
  
                @*@Html.DropDownListFor(Function(model) model.CODE_REF_ID, CType(ViewBag.RefCodesList, List(Of SelectListItem)), "-- SELECT ONE --", htmlAttributes:= New With {.class = "form-control", .id = "DDL_CODE_ID"})*@  
  
                @Html.ValidationMessageFor(model => model.REQ_REQUEST_DIV_ID, "", new { @class = "text-danger" })  
  
                @*See:https://social.msdn.microsoft.com/Forums/en-US/716cc01c-a89e-429d-b228-2e2d0df09fab/display-dropdownlist-text-in-capital-using-style?forum=aspmvc*@  
                @Html.ValidationSummary()  
            </div>  
        </div>  
  
  
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })  
        <div class="form-group">  
            @Html.LabelFor(model => model.REQ_REQUEST_TYPE_ID, htmlAttributes: new { @class = "control-label col-md-2" })  
            <div class="col-md-4">  
  
                @*@Html.DropDownListFor(model => model.REQ_REQUEST_TYPE_ID, new SelectList(ViewBag.DDL_Type, "RQT_REQUEST_ID", "RQT_REQUEST_DESCRIPTION"), "--SELECT ONE--", new { @class = "form-control", @id = "REQ_TYPE_DDL", @Onchange = "FilterSystemsDropList" })*@  
                @Html.DropDownListFor(model => model.REQ_REQUEST_TYPE_ID, new SelectList(ViewBag.DDL_Type, "RQT_REQUEST_ID", "RQT_REQUEST_DESCRIPTION"), "--SELECT ONE--", new { @class = "form-control", @id = "REQ_TYPE_DDL" })  
  
                @*@Html.EditorFor(model => model.REQ_REQUEST_TYPE_ID, new { htmlAttributes = new { @class = "form-control" } })*@  
                @Html.ValidationMessageFor(model => model.REQ_REQUEST_TYPE_ID, "", new { @class = "text-danger" })  
            </div>  
        </div>  

Sample of Partial Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace DUST.Models  
{  
  
    [MetadataType(typeof(WRITE_REQ_REQUEST_TBL_Metadata))]  
  
    public partial class WRITE_REQ_REQUEST_TBL { }  
  
    public class WRITE_REQ_REQUEST_TBL_Metadata  
    {  
  
        [DisplayName("REQUEST ID")]  
        public int REQ_REQUEST_ID { get; set; }  
  
        [DisplayName("REQUESTED TYPE")]  
        [Required(ErrorMessage = "Request Type Required")]  
        public int REQ_REQUEST_TYPE_ID { get; set; }  
  
  
        [DisplayName("REQUEST STATUS")]  
        [Required(ErrorMessage = "Request Status Required")]  
        public Nullable<int> REQ_REQUEST_STATUS_ID { get; set; }  
  
        [DisplayName("REQUESTED BY EMAIL ")]  
        [RegularExpression("^[a-zA-Z0-9_\\.-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$", ErrorMessage = "E-mail is not valid")]  
        [Required(ErrorMessage = "Email Is Required")]  
        public string REQ_REQUEST_EMAIL_ADDY { get; set; }  
  
        [DisplayName("REQUEST DETAILS")]  
        [Required(ErrorMessage = "Request Details Required")]  
        public string REQ_REQUEST_DETAILS { get; set; }  
  
  
  
         
        [DisplayName("REQUEST DATE")]  
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:g}")]  
        public Nullable<System.DateTime> REQ_REQUEST_DATE { get; set; }  
  
        [Required(ErrorMessage = "Completion Date Required")]  
        [DisplayName("REQUESTED COMPLETION DATE")]  
        [DataType(DataType.Date)]  
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]  
        public Nullable<System.DateTime> REQ_REQUEST_DUE_DATE { get; set; }  
  
  
        [DisplayName("REQUEST STATUS")]  
        public Nullable<bool> REQ_REQUEST_STATUS { get; set; }  
  
        [DisplayName("REQUEST CLOSE DATE")]  
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]  
        public Nullable<System.DateTime> REQ_REQUEST_CLOSE_DATE { get; set; }  
  
  
        [DisplayName("REQUEST LAST UPDATED")]  
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:g}")]  
        public Nullable<System.DateTime> REQ_REQUEST_STATUS_LAST_UPDATE { get; set; }  
  
  
        [DisplayName("REQUEST TAG")]  
        public string REQ_REQUEST_TAG { get; set; }  
  
        [DisplayName("REQUEST SYSTEM / SERVICE")]  
        [Required(ErrorMessage = "System / Service Selection Required")]  
        public int REQ_REQUEST_SYSTEM_ID { get; set; }  
  
  
  
  
        [DisplayName("REQUEST DIVISION")]  
        [Required(ErrorMessage = "Division Required")]  
        public int REQ_REQUEST_DIV_ID { get; set; }  
  
                       
    }  
  
}  

Sample of the Controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "REQ_REQUEST_ID,REQ_REQUEST_TYPE_ID,REQ_REQUEST_STATUS_ID,REQ_REQUEST_EMAIL_ADDY,REQ_REQUEST_DETAILS,REQ_REQUEST_DATE,REQ_REQUEST_DUE_DATE,REQ_REQUEST_STATUS,REQ_REQUEST_CLOSE_DATE,REQ_REQUEST_TAG,REQ_REQUEST_SYSTEM_ID,REQ_REQUEST_DIV_ID")] WRITE_REQ_REQUEST_TBL wRITE_REQ_REQUEST_TBL)
{

            DUST_TOOLS DustTools = new DUST_TOOLS();  
            WRITE_TOOLS WriteTools = new WRITE_TOOLS();  
  
  
            //Get Current User's Details  
            string CurrentUserNetworkID = DustTools.GetCurrentUser();  
            string CurrentUserEmail = DustTools.GetCurrentUserEmail(CurrentUserNetworkID);  
  
            if (ModelState.IsValid)  
            {  
                // This will clear whatever form items have been populated  
                ModelState.Clear();  
  
            }  
            else  
            {  
  
                try  
                {  
                    db.WRITE_REQ_REQUEST_TBL.Add(wRITE_REQ_REQUEST_TBL);  
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,292 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,956 Reputation points Microsoft Vendor
    2022-07-04T08:21:37.287+00:00

    Hi @App Dev ,

    I can't imagine what could have cause this to stop working

    You can press F12 to see if there is any specific error message.
    https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/hh968260(v=vs.85)
    According to your description and code, you should want to implement DropDownList validation, so I wrote a related demo, you can refer to it.
    Controller:

    public class HomeController : Controller  
        {  
            private DistrictContext db = new DistrictContext();  
            public IList<SelectListItem> GetCountries()  
            {                    
               var countries = db.Districts  
                    .Select(x => new SelectListItem { Text = x.DistrictName, Value = x.DistrictId.ToString() })  
                    .ToList();  
                countries.Insert(0, new SelectListItem { Text = "Choose a District", Value = "" });  
                return countries;  
            }  
             
            public ActionResult Index()  
            {  
                ViewBag.Districts = GetCountries();  
                return View();  
      
            }  
            [HttpPost]  
            [ValidateAntiForgeryToken]  
            public ActionResult Index(District district)  
            {  
                if (ModelState.IsValid)  
                {                 
                    ViewBag.Districts = GetCountries();  
                    return RedirectToAction("Index");  
                }  
                else  
                {  
                    try  
                    {  
                        ViewBag.Districts = GetCountries();  
                    }  
                    catch (Exception ex)  
                    {  
                    }  
                }  
                return View(district);  
            }  
        }  
    

    Model

     public class District  
        {  
            [Required(ErrorMessage = "Required.")]  
            [Display(Name = "Employee District:")]  
            public int? DistrictId { get; set; }  
      
            [Display(Name = "Employee District:")]  
            public string DistrictName { get; set; }  
      
        }  
    

    cshtml
    @默 WebApplication3.Models.District

    @{  
        ViewBag.Title = "NWR";  
    }  
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))  
    {  
        @Html.AntiForgeryToken()  
      
        <td>  
            <div class="editor-label">  
                @Html.LabelFor(model => model.DistrictId, "District")  
            </div>  
        </td>  
        <td>  
            <div class="editor-field">  
                @Html.DropDownListFor(model => model.DistrictId, new SelectList(ViewBag.Districts, "Value", "Text", new { @class = "form-control" }))  
                @Html.ValidationMessageFor(model => model.DistrictId)  
            </div>  
      
        </td>  
        <td><input type="submit" value="Submit" /></td>  
    }  
    

    217259-1234.gif

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. App Dev 86 Reputation points
    2022-07-07T21:10:28.127+00:00

    It turns out when I was making edits I deleted ehis section here which think was what poster Agavejoe posted?

    @Scripts.Render("~/bundles/jqueryval")  
    

    Not sure what it does 100%, but when I put it back it worked.
    Some how it was deleted when I was added a java scripting function to the page

    0 comments No comments