I have a problem with validation in ASP.NET MVC 2.0. I use the same Action in Controller to perform user request.
For example:
public ActionResult Index(ReportModel model)
{
if (!model.IsInitialDisplay && ModelState.IsValid)
{
model.Result = service.GetResult(model);
}
return View(model);
}
In the ReportModel, I define a flag IsInitialDisplay to determine whether the page is initial displayed or not:
public class ReportModel
{
[Required(ErrorMessage = "*")]
public string Criteria { get; set; }
public bool IsInitialDisplay { get; set; }
public ReportResult Result { get; set; }
public ReportModel()
{
IsInitialDisplay = true;
}
}
And in the View, I use the following code:
<% using (Html.BeginForm())
{ %>
<table>
<tr>
<th>
Criteria:
</th>
<td>
<%= Html.TextBox("Criteria", "") %>
<%= Html.ValidationMessage("Criteria") %>
</td>
</tr>
</table>
<br />
<input type="submit" value="Submit" />
<%= Html.Hidden("IsInitialDisplay", false) %>
<% } %>
As I expect, if users don't input any value for Criteria and click Submit button, the error message for validation will be displayed.
But the validation error message always displayed on initial page load, I don't know how to prevent it?
Does anyone know? Thanks,
[Updated]
I have updated my Action method as below and it's seem to be fine:
public ActionResult Index(ReportModel model)
{
// Collecting some commons data here...
if (model.IsInitialDisplay)
{
ModelState.Clear();
}
else if (ModelState.IsValid)
{
model.Result = service.GetResult(model);
}
return View(model);
}