gpt4 book ai didi

c# - 错误 : The view or its master was not found. 搜索了以下位置

转载 作者:太空宇宙 更新时间:2023-11-04 14:06:50 25 4
gpt4 key购买 nike

我对不显示 View 的特定 Controller 操作有疑问。

这是 Controller 操作:

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 名称是“Student”, View 存在于“Views/Student/DisplayStudents.aspx”

为什么我仍然会收到此错误:

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

另外,为什么它要查找“Thomas Guenn.aspx”而不是“DisplayStudents.aspx”?

这是我的查看页面:

>" %>

DisplayStudents Students are listed below:

<table>

<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.Encode(item)%>
</td>
</tr>

<% } %>

</table> </body> </html>

最佳答案

因为你的 View(name);调用正在调用过载 View(string viewName) .如果您想将字符串作为模型传递,请尝试使用

return View((object)name);

相反。

如果这不起作用,请尝试使用重载 View(string viewName, object model) 显式指定 viewName像这样:

return View("DisplayStudents", name);

UPD: 查看您的 View 代码,我可以说它也不会返回预期的页面:您从 Controller 返回单个字符串项作为模型,但是您的 DisplayStudents.aspx期待一个集合。您还应该更正您的 View (这样它将接受单个字符串作为模型)或返回 IEnumerable<string>来自 Controller - 通过删除 .First()来自 name 的 LINQ 表达式.

关于c# - 错误 : The view or its master was not found. 搜索了以下位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8733446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com