?
Solved

MVC pass a string between views

Posted on 2014-12-16
10
Medium Priority
?
62 Views
Last Modified: 2016-05-08
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
Comment
Question by:dizzycat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
10 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40502581
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
 

Author Comment

by:dizzycat
ID: 40502631
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
 

Author Comment

by:dizzycat
ID: 40502636
Sorry I mean display the Movie title in a label in another view
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 2000 total points
ID: 40502746
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
 

Author Comment

by:dizzycat
ID: 40502808
Thanks Carl, that's excellent.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40502835
No problem. Hope it answered your question.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 40503201
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
 

Author Comment

by:dizzycat
ID: 40503531
That works and is easier to understand, thanks again Carl.
0

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question