I have a view which I want to display, for which I have a controller which accepts a string (StudentID). Here is my controller action:
public ActionResult ShowStudent(string StudentID)
{
ViewData.Model = student.vwStudent.Where(s => s.StudentID == StudentID);
return View();
}
Now here, StudentId’s have leading zeros in them. e.g. 00003345, 000000223 etc.
Hence, to display the view for a particular student, I have to enter the exact string “00003345” in the url: “http://Students/ShowStudent/00003345” I want to be able to display the view even if I enter “3345” in the url: “http://Students/ShowStudent/3345” but I get an error.
I have tried passing a Long(Int64) parameter instead of a string and converting StudentID to String in ViewData.Model like:
ViewData.Model = student.vwStudent.Where(s => s.StudentID == StudentID.ToString());
But I get an error:
LINQ to Entities does not recognize the method ‘System.String ToString()’ method, and this method cannot be translated into a store expression.
EDIT:
What is wrong with just parsing it to an Int64 like this:
ViewData.Model = student.vwStudent.Where(s => Int64.Parse(s.StudentID) == Int64.Parse(StudentID));
Thanks all for the help, I took a bit of all of your advices and came to a solution that works for me.
I Was able to resolve this by Casting in my SQL:
And then converting it in my Controller: