我有这个控制器:
public class PostController : Controller
{
public ActionResult SimplePost(PostData postData, bool isRichView = true)
{
ViewBag.IsRichView = isRichView;
return View(postData);
}
public ActionResult VideoPost(PostData postData, bool isRichView = true)
{
ViewBag.IsRichView = isRichView;
return View(postData);
}
public ActionResult Post(PostData postData, bool isRichView = true)
{
switch (postData.PostType)
{
case PostType.SimplePost:
return SimplePost(postData, isRichView);
case PostType.VideoPost:
return VideoPost(postData, isRichView);
default:
throw new InvalidDataException("There is no post shell for this kind of post!");
}
}
}
在其他地方,我称之为:
foreach (var postData in RepositoryManager.PostRepository.GetList())
{
@Html.Action("Post", "Post", new {postData}) //Call Post method
}
所以在那之后我得到错误: 无法找到视图“发布”或示例,或者没有处理程序视图不支持搜索。 在以下位置搜索:
~/Views/Post/Post.aspx
~/Views/Post/Post.ascx
~/Views/Shared/Post.aspx
~/Views/Shared/Post.ascx
~/Views/Post/Post.cshtml
~/Views/Post/Post.vbhtml
~/Views/Shared/Post.cshtml
~/Views/Shared/Post.vbhtml
(大致从我的语言翻译成英语)。
但为什么会这样呢?
我从SimplePost和VideoPost方法调用View,为什么它会尝试查找“Post”视图?
答案 0 :(得分:1)
MVC知道行动Post
正在进行中。当您执行类似方法的操作时,它不会更改任何内容,就像此代码在Post
操作中一样。 MVC不会通知另一个操作正在执行。您需要在操作中添加视图名称以告诉MVC要呈现哪个视图:
...
public ActionResult SimplePost(PostData postData, bool isRichView = true)
{
ViewBag.IsRichView = isRichView;
return View("SimplePost", postData);
}
public ActionResult VideoPost(PostData postData, bool isRichView = true)
{
ViewBag.IsRichView = isRichView;
return View("VideoPost", postData);
}
...