在视图的引擎剃刀中传递参数作为模型

时间:2013-05-27 15:09:11

标签: c# asp.net asp.net-mvc razor

我想将模型传递给我,我试过这个

public ActionResult Properties(string projet) {
            string s = projet;
            if (projet != null)
                return View("Properties", s);
            return RedirectToAction("Index", "Akeo");

        }
此示例中的

projet具有值projet2。在程序启动时:出现此错误:~/Views/Akeo/Properties.aspx ~/Views/Akeo/Properties.ascx ~/Views/Shared/Properties.aspx ~/Views/Shared/Properties.ascx ~/Views/Akeo/Projet 2.master ~/Views/Shared/Projet 2.master ~/Views/Akeo/Projet 2.cshtml ~/Views/Akeo/Projet 2.vbhtml ~/Views/Shared/Projet 2.cshtml ~/Views/Shared/Projet 2.vbhtml 找不到其中一个视图但我创建了这样的视图Propertiesschema

我的错是什么?我怎么能纠正它?

2 个答案:

答案 0 :(得分:4)

应该是这样的:

return View("Properties", (object)s);

为什么?

因为this overload(这就是你所说的)之间存在差异:

protected internal ViewResult View(
    string viewName,
    string masterName
)

this overload(这就是你需要的):

protected internal ViewResult View(
    string viewName,
    Object model
)

问题来自于您的模型(s变量)是一个字符串,因此被解释为布局而不是模型。

答案 1 :(得分:2)

如果您的操作被调用Properties并且您的视图被称为Properties,那么您只需通过以下操作返回:

return View((object)s);

无需指定视图名称。仅当视图的名称与操作名称不匹配时,才需要指定视图名称。