• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 65
  • Last Modified:

MVC pass a string between views

Hi Guys

I am quite new to MVC and I am developing a little application for the purposes of learning, the problem I have is in my application I want to pass a string from a textbox in one view to a label in another  view, what is the best way to do this, I have thought of storing the string in a model or storing the string as a variable in a controller.  I am using MVC 4 and C#. Hope somebody out there has an example of how to do this.
0
dizzycat
Asked:
dizzycat
  • 4
  • 4
2 Solutions
 
Carl TawnSystems and Integration DeveloperCommented:
Could you clarify a little what you are trying to achieve?

What you are describing could equate to one of several things:

a) copying from a textbox to a label in current view (which could be done with client-side script)
b) post a value to the controller, and redisplay the same view with the value in a label
c) post a value to the controller, redirect to a different action that then presents a view with the submitted value
d) none of the above
0
 
dizzycatAuthor Commented:
Sorry for not making it clearer, I think i am trying to do:

c) post a value to the controller, redirect to a different action that then presents a view with the submitted value

What i want to do in my application is for the user to type in a movie title in a textbox and then click a button in one view, and then display all comments on that movie in a label in another view.
0
 
dizzycatAuthor Commented:
Sorry I mean display the Movie title in a label in another view
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Carl TawnSystems and Integration DeveloperCommented:
OK, quick example.

If you have a controller like:
namespace MvcApplication1.Controllers
{
    public class MovieController : Controller
    {
        //
        // GET: /Movie/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(string movieName)
        {
            return RedirectToAction("Details", new { movieName });
        }

        public ActionResult Details(string movieName)
        {
            // need to use a named parameter, otherwise it will assume the string
            // we are passing is the name of a view
            return View(model: movieName);
        }
    }
}

Open in new window

And two views:
// this is Index.cshtml
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm()) {
    <label for="movieName">Enter movie title:</label><input type="text" name="movieName" />
    <input type="submit" />
}

Open in new window

And:
// this is Details.cshtml
@model string

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<span>You entered: </span> @Model

Open in new window

That should then do what you want.
0
 
dizzycatAuthor Commented:
Thanks Carl, that's excellent.
0
 
Carl TawnSystems and Integration DeveloperCommented:
No problem. Hope it answered your question.
0
 
Carl TawnSystems and Integration DeveloperCommented:
Actually, just had a thought. Depending on your requirements you could actually simplify that example even more by changing the controller to:
    public class MovieController : Controller
    {
        //
        // GET: /Movie/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Details(string movieName)
        {
            // need to use a named parameter, otherwise it will assume the string
            // we are passing is the name of a view
            return View(model: movieName);
        }
    }

Open in new window

And changing Index.cshtml to:
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm("Details", "Movie")) {
    <label for="movieName">Enter movie title:</label><input type="text" name="movieName" />
    <input type="submit" />
}

Open in new window

0
 
dizzycatAuthor Commented:
That works and is easier to understand, thanks again Carl.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now