tags:

views:

64

answers:

6

Hi, 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 ?

A: 
return View("stringToPass");

you just need to pass your model, not the view's name or the controller.

Stefanvds
@Stefanvds passing a single string parameter into the view's constructor specifies the view's path, so at that point it's looking for "stringToPass.ascx" for the view file.
Parrots
+2  A: 

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"));
RememberME
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.
eglasius
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.
RememberME
@RememberME 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.
eglasius
+1  A: 

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.

p.campbell
+1 this will work. Note that the OP can still do it as he wanted, see my answer.
eglasius
ViewData[loosegooseybag] is so rarely the answer . ..
Wyatt Barnett
@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.
p.campbell
+1  A: 

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.

Parrots
+1  A: 

what about

 ViewData.Model = "StringToPass";
 return View("SomeView");
Pharabus
+3  A: 

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

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