I have a query that tests for a valid postcode entry:
using (_ctx)
{
try
{
var pc = _ctx.tblPostcodes.Where(z => z.Postcode == postcodeOutward)
.Select(x => new { postcodeId = x.PostcodeID }).Single();
pcId = pc.postcodeId;
}
catch (Exception)
{
pcId = 0;
Response.Redirect("./");
}
}
I don't like how I've done it. It's clumsy and it doesn't show an error (this is my first MVC project).
I'd rather it return a validation error against the Postcode textbox. I have model annotations for various input mistakes, but have to check the postcode against the database.
Any suggestions on how to set ModelState to get a proper response?