I have an issue with a particular controller action not displaying the view.
Here is the Controller Action:
public ActionResult DisplayStudents(string id)
{
var name = (from p in data.StudentTable.Where(a => a.ClassNumberID == id)
group n by n.StudentName into g
select g.Key).First();
return View(name);
}
Controller Name is “Student” and the view is present in “Views/Student/DisplayStudents.aspx”
Why do I still get this error:
Server Error in '/' Application.
--------------------------------------------------------------------------------
The view 'Thomas Guenn' or its master was not found. The following locations were searched:
~/Views/Student/Thomas Guenn.aspx
~/Views/Student/Thomas Guenn.ascx
~/Views/Shared/Thomas Guenn.aspx
~/Views/Shared/Thomas Guenn.ascx
Also, why is it looking for “Thomas Guenn.aspx” instead of “DisplayStudents.aspx” ?
Here is my View page:
>” %>
DisplayStudents
Students are listed below:
<table> <% foreach (var item in Model) { %> <tr> <td> <%= Html.Encode(item)%> </td> </tr> <% } %> </table> </body> </html>
Because your
View(name);call is calling overloadView(string viewName). If you want to pass string as Model then try usinginstead.
If this doesn’t work, try to specify viewName explicitly, using overload
View(string viewName, object model)like this:UPD: Looking at your view code, I can say that it won’t return expected page either: you’re returning a single string item as a model from controller, but your
DisplayStudents.aspxexpects a collection. You should also either correct your view (so it will accept single string as model) or return anIEnumerable<string>from controller – by removing.First()from LINQ expression forname.