skip to Main Content

**Hi, I’m using .Net 6 & visual studio 2022 community.

My Model validations through Data Annotations are working for all HTML tags, i want to use only for some fields..**

Code:

public class Student
    {
        public int Id { get; set; }
        [Display(Name="Student Name")]
        [System.ComponentModel.DataAnnotations.Required(ErrorMessage = "Please provide a value for Name field")]
        public string Name { get; set; }
        public string Email{ get; set; }
        public string Department{ get; set; }
    }
}

HTML:
I’m using Tag helpers to render HTML code.

@model MiddleWareComponents.Models.Student

@{
    ViewBag.Title = "Student";
}

<form asp-controller="home" asp-action="student" method="post" class="mt-3">

    <div class="form-group row">
        <label asp-for="@Model.Name" class="col-sm-2 col-form-label"></label>
        <div class="col-sm-10">
            <input asp-for="@Model.Name" class="form-control" placeholder="Name">
            <span asp-validation-for="@Model.Name" class="text-danger"></span>
        </div>
    </div>

 <div class="form-group row">
        <label asp-for="@Model.Department" class="col-sm-2 col-form-label"></label>
        <div class="col-sm-10">
            <input asp-for="@Model.Department" class="form-control" placeholder="department">
            
        </div>
    </div>

 <div class="form-group row">
        <label asp-for="@Model.Email" class="col-sm-2 col-form-label"></label>
        <div class="col-sm-10">
            <input asp-for="@Model.Email" class="form-control" placeholder="Email">
        
        </div>
    </div>

    <div class="form-group row">
        <div class="col-sm-10">
            <button type="submit" class="btn btn-primary">Create</button>
        </div>
    </div>
</form>



<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.12/jquery.validate.unobtrusive.js"></script>

**When we run the code, all html fields are required necessary to fill up , otherwise these fields are giving error and submit button not going to server. But there is only Name field is Required & mentioned in the model. **

Please give me a solution, how i can restrict some of the fields required to fill up , & some fields are not required. Thanks

2

Answers


  1. If your model state is not valid you want to return the view with the view-model instead of the empty string:

    [HttpPost]
    public string Student(Student student)
    {
        if (ModelState.IsValid)
        {
           return RedirectToAction("yourAction", "yourController");
        }
        else
        {
            return View(student);
        }
    }
    
    Login or Signup to reply.
  2. Accroding to your decription,I think you could check if you have referred the js files and if the js files are in the correct order,you could check this document related for more details:

        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.12/jquery.validate.unobtrusive.js"></script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search