Solved

MVC pass a string between views

Posted on 2014-12-16
10
54 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
  • 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
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to count occurrences of each item in an array.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now