Without selecting Study and Scenario DDL, when I click on save button, I get validation message "Study is required", "Scenario is required".
Now, when I select Study item and Scenario item from dropdown, these validation messages don't disappear on dropdown selectedIndex and inside ActionMethod, I get ModelState.IsValid = false due to which I am unable to save the records even after selecting the dropdown items.
Please let me know after selecting items why validation message doesn't disappear?
ViewModel
public class MappingWellViewModel
{
[Required(ErrorMessage = "Study is required")]
public string StudyId { get; set; }
public SelectList StudyList { get; set; }
[Required(ErrorMessage = "Scenario is required")]
public string ScenarioId { get; set; }
public SelectList ScenarioList { get; set; }
}
Well.cshtml
Study :
<select class="form-control col-md-8" onchange="GetScenario(this.value);"
asp-for="StudyId"
asp-items="@Model.StudyList">
<option value="">--Please Select--</option>
</select>
<span asp-validation-for="StudyId" class="text-danger"></span>
Scenario :
<select id="ddlScenario" class="form-control col-md-8" name="ddlScenario"
asp-for="ScenarioId"
asp-items="@Model.ScenarioList">
<option value="">--Please Select--</option>
</select>
<span asp-validation-for="ScenarioId" class="text-danger"></span>
<button type="submit" class="btn dxButton float-right">Save</button>
Controller
[HttpPost]
public IActionResult Well(MappingWellViewModel mappingWellViewModel)
{
try
{
if (ModelState.IsValid)
{
//save DDL selected values in DB
}
return Well();
}
catch(Exception ex)
{
throw ex;
}
}
I am working on Asp.Net Core MVC 3.1
Thank you in advance.