当我从控制器传递模型时,视图引擎不会渲染视图

本文关键字:视图 引擎 控制器 模型 | 更新日期: 2023-09-27 17:58:41

我正在使用C#和Razor开发MVC3应用程序。当我需要在播放视图中显示一个时,我遇到了问题。

Play操作方法用于检索FLV(Flash)文件的路径,然后将其传递到Play View以再现该文件。当我使用return View("Play")时,应用程序会正确地渲染视图。但是,我需要将路径变量传递给View,如代码中所示。当我这样做时,我会收到以下消息:

未找到视图"播放"或其主视图,或者没有视图引擎支持搜索的位置

以下是操作方法

public ActionResult Play(int topicId)
{
var ltopicDownloadLink = _webinarService.FindTopicDownloadLink(topicId);
if (ltopicDownloadLink != null)
{
    var path = Server.MapPath("~/App_Data/WebinarRecordings/" + ltopicDownloadLink);
    var file = new FileInfo(path);
    if (file.Exists)
    {
        return View("Play", path);
    }
}
return RedirectToAction("Index");
}

这是播放视图

@model System.String
<div id='player'>This div will be replaced by the JW Player.</div>
<script type='text/javascript' src='/FLV Player/jwplayer.js'></script>
<script type='text/javascript'>
   var filepath = @Html.Raw(Json.Encode(Model));
   jwplayer('player').setup({
   'flashplayer':'/FLV Player/player.swf',
   'width': '400',
   'height': '300',
   'file': filepath
   });
</script>

我唯一的提示是,我在javascript中使用模型时犯了一些错误。你能帮帮我吗?

感谢

当我从控制器传递模型时,视图引擎不会渲染视图

您调用了一个错误的重载。以下是正确的过载:

return View("Play", (object)path);

或者您也可以将path变量声明为对象:

object path = Server.MapPath("~/App_Data/WebinarRecordings/" + ltopicDownloadLink);

然后

return View("Play", path);

也将工作:

您应该将模型强制转换为对象return View("Play", (object)path);,否则第二个参数为主页面的路径的方法称为

视图重载的方式是,如果您将字符串(具有静态类型字符串)传递给它,它将进入错误的重载

你想要这个过载:

View(String, Object)    Creates a ViewResult object by using the view name and model that renders a view to the response.

这就是你实际调用的过载:

View(String, String)        Creates a ViewResult object using the view name and master-page name that renders a view to the response.

所以它认为你的模型就是母版页的名字。解决方法是让你传递的模型的静态类型不是字符串:

View("viewname",(object)model)

不知道为什么开发人员认为以如此模糊的方式重载View是个好主意。。。

在我看来,您需要在控制器中执行索引操作。

该错误与缺少索引操作有关,而与"播放"视图无关。尝试执行索引操作,看看会发生什么。