我知道这看起来很基本,而且应该是这样,但我无法找出哪里出错了。 (我已经阅读了关于 SO 的其他标题相似的文章,以及网络上的其他资源,但仍然无法弄明白),因此我们将不胜感激。
我有一个 Controller ,我在其中设置了一个字符串变量。现在我不介意这是采用属性、ActionResult 还是直接方法的形式。我只想要一个我可以在 Controller 中使用的简单字符串,并将其返回给 View 。
本质上,我要做的是列出给定文件夹中的文件。所以我的逻辑是这样的:
- 找到当前文件夹(部分成功)
将路径附加到您要放置的文件所在的位置。例如,如果我当前的文件夹是 Web\,那么如果我想列出所有 CSS 文件,我将附加类似“Content\CSS”的内容。 (我这样做是因为我想允许用户通过选择要应用的 css 来动态更改站点)。所以它看起来像:
CurrentPath += "Content\CSS"
我想将文件名加载到数组或列表中
- 我想将此列表传递到我的 View 以呈现在组合框中(在我的 _Layout.cshtml 上)。
重要的是要知道我正在尝试查看 _Layout.cshtml 上的字符串,因为我不能为其构建另一个 View 。 (除非我错了,否则我会感谢任何帮助)。
目前我仍在努力将一个简单的字符串传递到我的 View ,我可以像在第 2 步中那样自由地操作它。
我从一个单独的静态类和一个全局变量开始:
public static class MyTheme
{
public static string CurrentPath = HostingEnvironment.MapPath("~");
}
在我看来,我有: @Html.Label(MyProject.Web.Controllers.MyTheme.CurrentPath);
这行得通,但是当我尝试使用 if 语句来确定字符串是 null 还是空时,我遇到了错误。所以我接下来的尝试都失败了。
接下来,我决定将其放入 Controller (在本例中为我的 BaseController),这时我开始遇到问题。代码如下:
在 BaseController 类中
public ActionResult ThemePath()
{
string currentPath = Server.MapPath("~");
if (string.IsNullOrEmpty(currentPath))
{
currentPath = "Error!";
}
else
{
currentPath = "Our Path Is: " + currentPath;
}
return View(currentPath);
}
我不知道如何从我的 _Layout.cshtml View 中访问和运行它
接下来我在 BaseController 中尝试了一个标准方法:
public string ThemePath()
{
string currentPath = Server.MapPath("~");
if (string.IsNullOrEmpty(currentPath))
{
currentPath = "Error!";
}
else
{
currentPath = "Our Path Is: " + currentPath;
}
return currentPath;
}
同样,我不知道如何在 View 中访问它
最后我尝试使用 ViewBag 和 ViewData,现在我简直要疯了!所以在我的基本 Controller 中我有:
public string ThemePath()
{
ViewBag.currentPath = Server.MapPath("~");
if (string.IsNullOrEmpty(ViewBag.currentPath))
{
ViewBag.currentPath = "Error!";
}
else
{
ViewBag.currentPath = "Our Path Is: " + ViewBag.currentPath;
}
return ViewBag.currentPath;
}
在我看来我有
@Html.Label(ViewBag.CurrentPath);
甚至
@Html.Label(ViewBag.CurrentPath.ToString());
带有以下友好的小错误消息:
CS1973:“System.Web.Mvc.HtmlHelper”没有名为“Label”的适用方法,但似乎有一个名为“Label”的扩展方法。不能动态调度扩展方法。考虑在没有扩展方法语法的情况下转换动态参数或调用扩展方法。
最后我在 base 中尝试了 ViewData 如下: 公共(public)字符串 ThemePath() { ViewData["currentPath"] = Server.MapPath("~");
if (string.IsNullOrEmpty(ViewData["currentPath)"].ToString()))
{
ViewData["currentPath"] = "Error!";
}
else
{
ViewData["currentPath"] = "Our Path Is: " + ViewData["currentPath"];
}
return ViewData["currentPath"].ToString();
}
相应地在我尝试的 _Layout.cshtml 中:
@Html.Label(ViewData["CurrentPath"].ToString());
没有 .ToString() 我会得到上面的错误:
使用 .ToString() 我得到一个空引用执行错误。
所以我现在有点迷路了,真的不知道我哪里出错了。在此先感谢您的帮助。
最佳答案
要将字符串作为模型传递给 View ,您可以这样做:
public ActionResult Index()
{
string myString = "This is my string";
return View((object)myString);
}
您必须将它转换为一个对象,这样 MVC 就不会尝试加载该字符串作为 View 名称,而是将其作为模型传递。你也可以这样写:
return View("Index", myString);
.. 这有点冗长。
然后在您的 View 中,将其作为字符串键入:
@model string
<p>Value: @Model</p>
然后您可以按照自己的意愿操作模型。
为了从布局页面访问它,最好为此创建一个 HtmlExtension:
public static string GetThemePath(this HtmlHelper helper)
{
return "/path-to-theme";
}
然后在你的布局页面中:
<p>Value: @Html.GetThemePath()</p>
希望您能将此应用到您自己的场景中。
编辑:显式 HtmlHelper 代码:
namespace <root app namespace>
{
public static class Helpers
{
public static string GetThemePath(this HtmlHelper helper)
{
return System.Web.Hosting.HostingEnvironment.MapPath("~") + "/path-to-theme";
}
}
}
那么在你看来:
@{
var path = Html.GetThemePath();
// .. do stuff
}
或者:
<p>Path: @Html.GetThemePath()</p>
编辑 2:
如前所述,如果您添加一个 @using
,Helper 就会工作。声明到你的 View 的顶部,命名空间指向你的助手所在的那个。
关于c# - 将一个简单的字符串从 Controller 传递到 View MVC3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11296634/