简体   繁体   中英

Data annotation validation happening on page load in ASP.Net Core

I am facing the issue of data validation being executed on load of a new page even though it is clearly coming from a Get method. Is there something that's triggering the validations on page load?

I have a button on a view to add a new Student record in the new screen:

View:

<a type="button" id="btnAddStudent" href='@Url.Action("Details","Student")' class="btn btn-tertiary" title="Add Student">Add Student</a>

The controller code for the Details action method in Student Controller is as follows.

[HttpGet]
public ActionResult Details(StudentInfo model)
{
    //This is populating the model parameters as expected.
    helper.StudentInfo(ref model);  
     return View(model);
}

The view for the Details screen is as follows. The page loads but is throwing validation errors even though it's a Get method.

<form id="frmSubmit" asp-action="Details" asp-controller="Student" method="post">
<input type="hidden" asp-for="StudentId" />

<div class="row">
    <div class="col-xs-12">
        @Html.ValidationSummary("", new { @class = "alert alert-danger validation" })
    </div>
</div>

<div class="row">
    <div class="col-xs-12">
        <div class="form-group">
            <label asp-for="Name">*StudentName</label><br />
            <input asp-for="Name" class="form-control" maxlength="100"  placeholder="Enter student name..." />
            <span asp-validation-for="Name" class="text-danger"></span>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-xs-12">
        <div class="form-group">
            <label asp-for="AddressLine1"></label><br />
            <input asp-for="AddressLine1" class="form-control" placeholder="Enter address..." />
            <span asp-validation-for="AddressLine1" class="text-danger"></span>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-xs-12">
        <div class="form-group">
            <label asp-for="AddressLine2"></label><br />
            <input asp-for="AddressLine2" class="form-control" maxlength="100" />
            <span asp-validation-for="AddressLine2" class="text-danger"></span>
        </div>
    </div>
</div>

<div class="box">
    <div class="form-group pull-right">           
        <button type="submit" class="btn btn-primary" value="save"> Save</button>
    </div>
</div>  

Is there something I am doing wrong? I have verified that the debug control goes to the Get method.There's alos no on load scripts which are doing any sort of validation.

1.Your get method contains the model parameter, when the request hit the method it will judge the ModelState by default. And when you hit the get method by your shared <a> , it send request without any data, so the ModelState is invalid.

2.Default Tag helper displays ModelState's value not Model.

In conclusion, you will render the ModelState error although it is a get method.

Two ways you can resolve this problem. The first way is that you can add ModelState.Clear() before you return View:

public ActionResult Details(StudentInfo model)
{
    ModelState.Clear();  //add this....
    helper.StudentInfo(ref model); 
            
    return View(model);
}

The second way is do not add the model as parameter:

public ActionResult Details()
{
    var model = new StudentInfo();
    helper.StudentInfo(ref model);
           
    return View(model);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM