For the life of me I cant figure out why this simple piece of code is not working. I know there are many questions like this here and I 've tried all of them, but I just cant get it to work!
My controller:
//validate email
if (studentlist.StudentLiveId == null || !studentlist.StudentLiveId.Contains("@"))
{
ModelState.AddModelError("StudentLiveId", "Please enter a valid email id");
return View(studentlist);
}
My View - Relevant bit:
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<p style="font-weight: bold; font-size: large; color: red">
hELLO
</p>
<div class="editor-field">
@Html.ValidationMessageFor(model => model.StudentLiveId)
</div>
The error:
If I leave the liveid field null , instead of showing an error and stopping, it just skips over the Live Id validation field and goes to some other dropdown field in the model and throws a Object reference not set to instance of an object error!
I am just printing the validation error in another common place and not next to the relevant field. I expect the view to throw the error and stop loading further and not go to the dropdown and show some random error. What is wrong here?