required attribute not working in asp.net mvc

前端 未结 6 1559
离开以前
离开以前 2020-12-03 19:53

I have a simple strongly-typed view.

@model GoldForGold.Models.LogonModel
@{
    ViewBag.Title = \"Logins\";
    Layout = \"~/Views/Shared/_Layout.cshtml\";         


        
6条回答
  •  误落风尘
    2020-12-03 20:28

    Here is the complete solution:

    1. Add the following scripts to your view:

          
      
      
      
    2. Enable Validation in Web.Config:

      
      
      
      
      
    3. Add validation element in the view:

      @Html.ValidationMessageFor(model => model.UserName)
      
    4. Add Required attribute to you model`s element.

      [Required(ErrorMessage="please enter username")]
      public string UserName { get; set; }
      

提交回复
热议问题