I get the following error when returning a view:
Server Error in '/' Application.
--------------------------------------------------------------------------------
The view 'student' or its master was not found. The following locations were searched:
~/Views/Student/student.aspx
~/Views/Student/student.ascx
~/Views/Shared/student.aspx
~/Views/Shared/student.ascx
Here is my controller action:
[HttpPost]
public ActionResult SubmitStudent()
{
StudentViewModel model = TempData["model"] as StudentResponseViewModel;
ViewData["id"] = model.Id;
ViewData["name"] = model.Name;
string comment = Request["comment"];
var student = student.studentTable.Where(s => s.studentId == model.Id);
return View(student);
}
Here is my View:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<string>>" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Student</title>
</head>
<body>
<div>
Student name listed below:
</div>
<table>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.Encode(item)%>
</td>
开发者_JAVA百科</tr>
<% } %>
</table>
</body>
</html>
A few things to consider here.
First of all, returning a view after a HTTP POST is really a bad design choiche. You can google about the PRG Pattern and you will find many articles that will explain why you should always redirect to a HTTP GET which will render your view.
Second, I find strange that your code is looking for a view name "student". As per MVC specification, the controller will look for a view named as the action method unless an overload of the View() method which accepts the view name as parameter is called (which is not your case, at least not in the code you posted).
In your example, it seems like it should look for a view named "SubmitStudent". Again, the model type you declare on your view doesn't match the model you're passing to it. It accepts an IEnumerable<string>
but you're passing to it an IQueryable<Student>
(that's what your student variable contains).
I think you omitted some parts of your code. The parts you posted don't quite match with one another.
In order for your code to work, you're going to need a view called SubmitStudent.aspx
inside the Views\Student\
or Views\Shared\
folders.
It also looks odd that your view inherits a list of strings and not a Student
object or whatever type of object your query returns. Your view is expecting an enumerable list of string
's
This line is also confusing:
var student = student.studentTable.Where(s => s.studentId == model.Id);
Did you mean:
var student = model.studentTable.Where(s => s.studentId == model.Id);
Your view must be in "Views\Student\"
- unless you have changed the view engine settings which I imagine you have not.
So I believe your view is not there.
精彩评论