将一个简单的字符串从控制器传递给视图MVC3

Fra*_*ers 39 c# asp.net-mvc viewbag asp.net-mvc-3

我知道这看起来非常基本,而且应该是,但是我无法找出我出错的地方.(我已经在SO上阅读了其他类似标题的文章,以及网络上的其他资源,但仍然无法弄清楚),所以任何帮助都将受到赞赏.

我有一个控制器,在其中我设置一个字符串变量.现在我不介意它是采用属性,ActionResult还是直接方法的形式.我只想要一个简单的字符串,我可以在控制器中玩,并将其返回到视图.

基本上我要做的是列出给定文件夹中的文件.所以我的逻辑是这样的:

  1. 找到当前文件夹(部分成功)
  2. 将路径附加到要查找的文件所在的位置.即如果我当前的文件夹是Web \,那么如果我想列出所有CSS文件,我会附加类似"Content\CSS"的内容.(我这样做是因为我想允许用户通过选择要应用的CSS来动态更改网站).所以它看起来像:

    CurrentPath + ="Content\CSS"

  3. 我想将文件名加载到数组或列表中

  4. 我想将此列表传递给我的视图以在组合框中呈现(在我的_Layout.cshtml上).

重要的是要知道我正在尝试查看_Layout.cshtml上的字符串,因为我无法为它构建另一个视图.(除非我错了,在这种情况下我会提供任何帮助).

目前我仍在努力将一个简单的字符串传递给我的视图,我可以像在第2步中那样自由地操作它.

我从一个单独的静态类和一个全局变量开始:

public static class MyTheme
{
    public static string CurrentPath = HostingEnvironment.MapPath("~");
}
Run Code Online (Sandbox Code Playgroud)

在我看来,我有:@ Html.Label(MyProject.Web.Controllers.MyTheme.CurrentPath);

这工作但当我尝试使用if语句来确定字符串是否为空或我是否有错误.所以我的下一次尝试都失败了.

接下来我决定将它带入一个控制器(在本例中是我的BaseController),这是我开始遇到问题的时候.代码如下:

在BaseController类中

    public ActionResult ThemePath()
    {
        string currentPath = Server.MapPath("~");

        if (string.IsNullOrEmpty(currentPath))
        {
            currentPath = "Error!";
        }
        else
        {
            currentPath = "Our Path Is: " + currentPath;
        }

        return View(currentPath);
    }
Run Code Online (Sandbox Code Playgroud)

我不知道如何从我的_Layout.cshtml视图中访问和运行它

接下来我在BaseController中尝试了一个标准方法:

    public string ThemePath()
    {
        string currentPath = Server.MapPath("~");

        if (string.IsNullOrEmpty(currentPath))
        {
            currentPath = "Error!";
        }
        else
        {
            currentPath = "Our Path Is: " + currentPath;
        }

        return currentPath;
    }
Run Code Online (Sandbox Code Playgroud)

我再次不知道如何在视图中访问它

最后我尝试使用ViewBag和ViewData,现在我只是疯了!所以在我的基本控制器中我有:

    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;
    }
Run Code Online (Sandbox Code Playgroud)

在我看来,我有

     @Html.Label(ViewBag.CurrentPath);
Run Code Online (Sandbox Code Playgroud)

甚至

     @Html.Label(ViewBag.CurrentPath.ToString());
Run Code Online (Sandbox Code Playgroud)

使用以下友好的小错误消息:

CS1973:'System.Web.Mvc.HtmlHelper'没有名为'Label'的适用方法,但似乎有一个名称的扩展方法.无法动态分派扩展方法.考虑转换动态参数或调用扩展方法而不使用扩展方法语法.

最后我在基础上尝试了ViewData,如下所示:public string 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();
    }
Run Code Online (Sandbox Code Playgroud)

并相应地在_Layout.cshtml中尝试过:

     @Html.Label(ViewData["CurrentPath"].ToString());
Run Code Online (Sandbox Code Playgroud)

没有.ToString()我得到上面的错误:

使用.ToString(),我得到一个null引用的execption错误.

所以我现在有点失落,并不知道我哪里出错了.在此先感谢您的帮助.

Ste*_*bbs 90

要将字符串作为模型传递给视图,您可以执行以下操作:

public ActionResult Index()
{
    string myString = "This is my string";
    return View((object)myString);
}
Run Code Online (Sandbox Code Playgroud)

您必须将其强制转换为对象,以便MVC不会尝试将字符串作为视图名称加载,而是将其作为模型传递.你也可以这样写:

return View("Index", myString);
Run Code Online (Sandbox Code Playgroud)

..这有点冗长.

然后在您的视图中,只需将其键入为字符串:

@model string

<p>Value: @Model</p>
Run Code Online (Sandbox Code Playgroud)

然后你可以按照你想要的方式操纵Model.

要从布局页面访问它,最好为此创建一个HtmlExtension:

public static string GetThemePath(this HtmlHelper helper)
{
    return "/path-to-theme";
}
Run Code Online (Sandbox Code Playgroud)

然后在你的布局页面内:

<p>Value: @Html.GetThemePath()</p>
Run Code Online (Sandbox Code Playgroud)

希望您可以将此应用于您自己的场景.

编辑:显式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";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在你看来:

@{
    var path = Html.GetThemePath();
    // .. do stuff
}
Run Code Online (Sandbox Code Playgroud)

要么: <p>Path: @Html.GetThemePath()</p>

编辑2:

如上所述,如果您@using在视图的顶部添加一个语句,并且命名空间指向您的帮助程序所在的命名空间,则Helper将起作用.

  • 是的,它确实.`@ model`表示您正在为文档设置Model类型,`@ Model`是您实际引用类型并访问其数据的方式.`@ model`通常是你在视图中指定的第一件事,在最顶层. (3认同)

Jay*_*ney 11

使用ViewBag

ViewBag.MyString = "some string";
return View();
Run Code Online (Sandbox Code Playgroud)

在你的视图中

<h1>@ViewBag.MyString</h1>
Run Code Online (Sandbox Code Playgroud)

我知道这不能回答你的问题(已经回答了),但你的问题的标题非常广泛,可以让这个页面上的任何人正在搜索一个查询,将一个简单的字符串传递给View from Controller.


Sae*_*eas 5

为什么不使用简单的字符串参数创建一个视图模型,然后将其传递给视图?它具有可扩展的优点(即,您可以添加任何其他您可能想要在控制器中设置的内容),而且相当简单。

public class MyViewModel
{
    public string YourString { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在视图中

@model MyViewModel
@Html.Label(model => model.YourString)
Run Code Online (Sandbox Code Playgroud)

在控制器中

public ActionResult Index() 
{
     myViewModel = new MyViewModel();
     myViewModel.YourString = "However you are setting this."
     return View(myViewModel)
}
Run Code Online (Sandbox Code Playgroud)

  • 试试 Html.LabelFor,这可能会奏效。我通常不使用 C#,所以我很可能在几个地方搞砸了语法。 (2认同)