Pass a simple string from controller to a view MVC3

前端 未结 6 1702
青春惊慌失措
青春惊慌失措 2021-01-31 02:19

I know this seems pretty basic, and it should be, but I cant find out where I am going wrong. (I hve read other articles with similar titles on SO, and other resources on the we

相关标签:
6条回答
  • 2021-01-31 02:41

    If you are trying to simply return a string to a View, try this:

    public string Test()
    {
         return "test";
    }
    

    This will return a view with the word test in it. You can insert some html in the string.

    You can also try this:

    public ActionResult Index()
    {
        return Content("<html><b>test</b></html>");
    }
    
    0 讨论(0)
  • 2021-01-31 02:46

    To pass a string to the view as the Model, you can do:

    public ActionResult Index()
    {
        string myString = "This is my string";
        return View((object)myString);
    }
    

    You must cast it to an object so that MVC doesn't try to load the string as the view name, but instead pass it as the model. You could also write:

    return View("Index", myString);
    

    .. which is a bit more verbose.

    Then in your view, just type it as a string:

    @model string
    
    <p>Value: @Model</p>
    

    Then you can manipulate Model how you want.

    For accessing it from a Layout page, it might be better to create an HtmlExtension for this:

    public static string GetThemePath(this HtmlHelper helper)
    {
        return "/path-to-theme";
    }
    

    Then inside your layout page:

    <p>Value: @Html.GetThemePath()</p>
    

    Hopefully you can apply this to your own scenario.

    Edit: explicit HtmlHelper code:

    namespace <root app namespace>
    {
        public static class Helpers
        {
            public static string GetThemePath(this HtmlHelper helper)
            {
                return System.Web.Hosting.HostingEnvironment.MapPath("~") + "/path-to-theme";
            }
        }
    }
    

    Then in your view:

    @{
        var path = Html.GetThemePath();
        // .. do stuff
    }
    

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

    Edit 2:

    As discussed, the Helper will work if you add a @using statement to the top of your view, with the namespace pointing to the one that your helper is in.

    0 讨论(0)
  • 2021-01-31 02:54

    Just define your action method like this

    public string ThemePath()
    

    and simply return the string itself.

    0 讨论(0)
  • 2021-01-31 03:01

    @Steve Hobbs' answer is probably the best, but some of your other solutions could have worked. For example, @Html.Label(ViewBag.CurrentPath); will probably work with an explicit cast, like @Html.Label((string)ViewBag.CurrentPath);. Also, your reference to currentPath in @Html.Label(ViewData["CurrentPath"].ToString()); is capitalized, wherein your other code it is not, which is probably why you were getting null reference exceptions.

    0 讨论(0)
  • 2021-01-31 03:07

    Use ViewBag

    ViewBag.MyString = "some string";
    return View();
    

    In your View

    <h1>@ViewBag.MyString</h1>
    

    I know this does not answer your question (it has already been answered), but the title of your question is very vast and can bring any person on this page who is searching for a query for passing a simple string to View from Controller.

    0 讨论(0)
  • 2021-01-31 03:07

    Why not create a viewmodel with a simple string parameter and then pass that to the view? It has the benefit of being extensible (i.e. you can then add any other things you may want to set in your controller) and it's fairly simple.

    public class MyViewModel
    {
        public string YourString { get; set; }
    }
    

    In the view

    @model MyViewModel
    @Html.Label(model => model.YourString)
    

    In the controller

    public ActionResult Index() 
    {
         myViewModel = new MyViewModel();
         myViewModel.YourString = "However you are setting this."
         return View(myViewModel)
    }
    
    0 讨论(0)
提交回复
热议问题