5

I want to pass a string object into a View:

 <%@ Page Title="" Language="C#" 
     MasterPageFile="~/Views/Shared/Site.Master"
     Inherits="System.Web.Mvc.ViewPage<String>" %>

   <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

          <h2><%=Model %></h2>

   </asp:Content>

When I try this:

 return View("SomeView", "stringToPass");

the error occurs: The view 'SomeView' or its master was not found.

but, when I change the return to

return View("SomeView");

everything works fine. So how to pass that string ?

6 Answers 6

11

its confusing it with another overload of View(), do:

return View("SomeView", (object)"stringToPass");
Sign up to request clarification or add additional context in comments.

Comments

4

Use ViewData for this. In your Controller, simply set the key/value pair:

ViewData["Foo"] = "bar";

Then in your View, just access it just as you'd set it previously:

<h2><%=ViewData["Foo"]%></h2>

The problem you were having is that the View() method's 2 parameters are : View name and Master name.

2 Comments

+1 this will work. Note that the OP can still do it as he wanted, see my answer.
@Wyatt: indeed, but it looks like it worked for the OP. Curious about getting a downvote for the straightforward answer though. :| Not to suggest it was you. How it could get a -1 for being an accepted answer is baffling.
3

what about

 ViewData.Model = "StringToPass";
 return View("SomeView");

Comments

2

The View class has a constructor of View("string1", "string2") where string1 is the view's name and string2 is the master page's name. The problem is you're passing in two strings so it's assuming you mean to call that overloaded method.

Comments

2

If you're within your "SomeView" action method:

return View("StringToPass");

If you're within a different action method:

return RedirectToAction("SomeView", new { x = "StringToPass" });

EDIT I guess option 1 won't work with a string. I've never tried it b/c I always use ViewModels:

public class UserAdminViewModel
    {
        public string UserName { get; private set; }

        public UserAdminViewModel(string userName)
        {
            UserName = userName;
        }
    }

Then you would

return View(new UserAdminViewModel("StringToPass"));

3 Comments

this is wrong, the first will look for StringToPass view, and I don't think the later would work ... just saw the edit -> you can definitely pass a string to the view without a ViewModel.
The RedirecttoAction works assuming you have an action method which takes the x string. With the ViewModel, my cases are more complex than simply setting the string.
yes, didn't mean it to look like I don't like view models, I definitely use them, but you can certainly pass a simple string value as the model if you want.
0

Old question, but I'll give an example of how to pass string to the View using a model (not using Viewbag which has already been answered)

    // Action Method inside controller
    public ActionResult Index()
    {
        string msg = "Hello World";
        return View("Index","",msg); // notice the blank second parameter
    }

    // This goes inside the Index View
    // Will print, The Message is: Hello World
    The Message is: @Model.ToString()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.