1

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

3 Answers 3

3

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"]) %>
Sign up to request clarification or add additional context in comments.

3 Comments

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).
+1 Exactly. And by far the best approach rather than using collection["???"]; in my opinion.
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
1

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();
}

1 Comment

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
1

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;

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.

10 Comments

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.
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?
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
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.
yes there is. use the 1st method in your previous question and use collection["strTest"] though i do not recommend.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.