When StudentId is passed as a parameter in controller action, how can I validate if the id passed is present in StudentTable in a controller action?
public ActionResult LookUpStudentId(string id)
{
if(id != //not present in StudentTable)
return new RedirectResult("~/Error/NotFound");
return View();
}
The answer to your question will very much depend on the data access technology you are using to access your SQL server. Because you haven’t told us about it here’s how to achieve this using plain ADO.NET:
and then:
Obviously this data access code is better to be refactored in a repository so that your controller is not tightly coupled to the data access technology you are using. So for example you would define a
IStudentsRepository:which you would then implement and now your controller action could work with this abstraction:
All that is left is to configure your DI framework of choice to inject the proper implementation of this repository into the controller.