ModelState.AddModelError is not being displayed inside my view

I would urge you to change your try{ } catch(){ }

And first check if there exists a visit for the given id
and if so simply returns the model with the added model error

    if (visitExists)
    {
         ModelState.AddModelError("CustomError", "The Same test Type might have been already created,, go back to the Visit page to see the avilalbe Lab Tests");
         return View(vlr);    
    }
    //Other code here

Change your AddModelError To

ModelState.AddModelError("CustomError", "The Same test Type might have been already created,, go back to the Visit page to see the avilalbe Lab Tests");

And in your view simply add a

@Html.ValidationMessage("CustomError")

Then when you return your model the error will be shown where you have placed the @Html.ValidationMessage …

Leave a Comment