views:

371

answers:

3

Hello, How do I use multiple actions on the same controller?

I'm using the default project that comes up when opening a new project in asp.net mvc.

I added one more Index action on the homecontroller to accept a value from a textbox...like this

 string strTest;
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(FormCollection frm)
        {
            strTest = frm["testbox"];

            return RedirectToAction("Index");
        }

Now,I need to display the entered value back to the user. How do I do this?

I tried this..

public ActionResult Index()
{
    this.ViewData.Add("ReturnMessage", strValue);
    return View();
}

Here's what I've put on my view..

<% using (Html.BeginForm())
   { %>
<p>
    <%=Html.TextBox("testbox")%>
</p>
<p>
    <input type="submit" value="Index" /></p>
<p>
    <%= Html.ViewData["ReturnMessage"] %>
</p>
<% } %>

the compiler typically doesn't let me add another index with same constructor to display the entered message back to the user which is obvious in c# I know. But,then how do I get the message back out to the user. Thanks

+1  A: 

Josh, see the previous question you asked.

In there I had <%= Html.textbox("myInput", Model.myInput....

it's the Model.myInput that will put the value from your model into the text of yoru text box.

EDIT

Or if you don't want it in a text box then simply do;

<%= Model.myInput %>

EDIT 2

You can add as many items into your new form view model and it has, in this case, nothing to do with a database. see your previous question on where i declared the class.

the class can have as many properties as you like. So you can add a string myResponse {get;set;} to return a response back to your view so then you can use <%=Model.myResponse%>

Hope this helps.

griegs
I know...but for now..I'm just doing a test without a model...because I need to teach someone else who doesn't know mvc at all.
Josh
I think model in the sense you mean't that I need to import a database into the project and build an edmx file etc right?
Josh
I still think you should teach it this way as it is the prefered way. I'm actually not a big fan of ViewData but that's personal. I'd always prefer the model approach. See Edit 2
griegs
No no no. see my edit #2 - typing it now.
griegs
or create a class like that and put it inside the models folder....For now,I need to just display back the string. I'm looking for shortcut ways because I'm a beginner and I've taken the challenge to train one more person and make them work on it.
Josh
Just give me a sec...let me try it out here...sorry for giving you all the trouble...
Josh
Beleive me Josh. Getting this right right now will help you no end later on down the track. If you get this right then the rest becomes a whole lot easier.
griegs
Griegs,I don't have a model right now on this sample.I haven't created one.I'm trying your first approach.The viewpage starts with this <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
Josh
And I have string strTest on my controller...Did you see the code I've put on the controller? I'm trying to catch its value. Is there no way to do it without using a model?
Josh
That's right but now add the rest which says something like Inherits="System.Web.Mvc.ViewPage<YOURNAMESPACE.Controllers.YOURFORMVIEWMODELCLASSNAME>"Your namespace is abviouse. The FormViewModelName is the name of the class that contains your fields. Check my first edit on your previous question. For simplicities sake i placed the class within your controller.
griegs
yes there is. use the 1st method in your previous question and use collection["strTest"] though i do not recommend.
griegs
+3  A: 

Well, a controller matches one route, based on the parameters sent. You can layer your routes from most specific to least specific, it checks in order. First one that hits wins.

The other answer is to either strongly type your model sent to your view, or store it in the ViewData:

ViewData["Message"] = "Welcome to ASP.NET MVC!";

Then access it in your View:

<%= Html.Encode(ViewData["Message"]) %>
Dr. Zim
The View accepts one model, which has nothing to do with databases or anything else other than it stores data for the view. You can overload that model by defining a class then "Inherit" that class in the view. The overloaded class can contain multiple sets of data, like a list of cities and a list of products (anything the view requires).
Dr. Zim
+1 Exactly. And by far the best approach rather than using collection["???"]; in my opinion.
griegs
ViewData["Message"] thing does not work on my postback action. If a user enters their name in a text box,i want to greet them with Hello and their name
Josh
+1  A: 

Simple method

In your view

<% using (Html.BeginForm()) {%>
    <%= Html.TextBox("myInput") %>
    <%= ViewData["response"] %>
<%}%>

In your controller;

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

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection collection)
{
  ViewDate.Add("response", collection["myInput"]);
  return View();
}
griegs
got it...sorry,that was a silly mistake I made...since I was also returning data from database the entity framework way.I returned view() instead of passing that data back.so it threw the object reference exception.Thanks again
Josh
No probs Josh. Glad we got you working.
griegs