Solved

MVC pass a string between views

Posted on 2014-12-16
10
61 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
Independent Software Vendors: 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!

 
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
 

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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

688 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