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

4

3 に答える 3

3

コントローラーは、送信されたパラメーターに基づいて、1 つのルートを照合します。ルートを最も具体的なものから最も具体的でないものまで階層化でき、順番にチェックします。最初に当たった方が勝ち。

もう 1 つの答えは、ビューに送信されるモデルを厳密に型指定するか、ViewData に保存することです。

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

次に、ビューでアクセスします。

<%= Html.Encode(ViewData["Message"]) %>
于 2009-08-31T03:47:41.503 に答える
1

簡単な方法

あなたの見解では

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

コントローラーで;

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

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection collection)
{
  ViewDate.Add("response", collection["myInput"]);
  return View();
}
于 2009-08-31T04:22:13.133 に答える
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.

于 2009-08-31T03:43:37.173 に答える