[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

MVC pass a string between views

Posted on 2014-12-16
10
Medium Priority
?
63 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

656 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