我正在构建一个从数据库中的视图返回信息的函数。
我的控制器看起来像是
[HttpPost]
public ActionResult StudentsLookup(string Firstname, string Lastname)
{
//checks for vaild name
var nameCount = db.v_Directory.Where(name => name.LAST_NAME == Lastname).Count();
//searches by lastname
if (string.IsNullOrEmpty(Firstname) && nameCount > 0)
{
object c = db.v_Directory.Where(name => name.LAST_NAME == Lastname).ToList();
return View("StudentsLookup", c);
}
//searches by fullname
if (!string.IsNullOrEmpty(Firstname) && nameCount > 0)
{
object l = db.v_Directory.Where(name => name.LAST_NAME == Lastname && name.FIRST_NAME == Firstname).ToList();
return View("StudentLookup", l);
}
else
{
ViewBag.Message = "Invalid Search";
return View("Index");
}
}
仅搜索姓氏的函数返回有效视图,搜索名字和姓氏将返回异常。我是新编码而且很困惑。在两种情况下,调试显示通过模型传输的数据。
View看起来像这样:
@{
ViewBag.Title = "StudentsLookup";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>StudentsLookup</h2>
@{
if (Model != null)
{
<h1>Possible Matches</h1>
<table>
<tr>
<th> STUDENT_ID </th>
<th> FirstName </th>
<th> LastName </th>
<th> USER_NAME </th>
</tr>
@foreach (var person in Model)
{
<tr>
<td>@person.STUDENT_ID</td>
<td>@person.FIRST_NAME </td>
<td>@person.LAST_NAME </td>
<td>@person.USER_NAME</td>
</tr>
}
</table>
}
}
我收到错误,InvalidOperationException:找不到视图'StudentLookup'或其主人,或者没有视图引擎支持搜索的位置。搜索了以下位置: Message ='bool'不包含'STUDENT_ID'的定义
非常感谢任何帮助。