Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want some help in Asp.net MVC 3

I have a controller named Form,in which i have 2 actions

SQL
[HttpGet]
       public ActionResult Index()
       {
           return View();
       }

       [HttpPost]
       public ActionResult Index(FormModel f)
       {
           var txt = new TextBox
           {
               Required = true,
               Title = f.ID,
               Prompt = "Enter ID",
               RequiredMessage = "ID is required"

           };
           return View();
       }


On First Action(ie HttpGet one) I am returning a view with a textbox and a submit button,
I want a functionality so that when i click on the submit button,the second Action will call(ie HttpPost one)

How can i do this?
Routing URL is not the solution for this :(
Posted

Within your View code, you can always tell your form which controller and action you want to post to

For example, razor syntax would be

@using (Html.BeginForm("Index", "Form"))
{
<div class="editor-field">
    @Html.HiddenFor(model => model.SomeField)
</div>
}
 
Share this answer
 
Hi Syed,

Just add a view by right clicking -> Add View.

Now you just have to do is write following in your view.cshtml page

<form id="FormID" action="/Form/Index" method="post">


Let me know if any issue...

-Sagar Solanki
 
Share this answer
 
Since you are referring Model, Please make sure you are using @Html Helper Class to construct your Views, only then the Value will be set to Model when you do POST.

PHP
@using (Html.BeginForm())
{
   <label for="firstName">First Name:</label>
   <br />
   @Html.TextBox("firstName")
   <br /><br />
   <label for="lastName">Last Name:</label>
   <br />

   @Html.TextBox("lastName")
   <br /><br />
   <input type="submit" value="Register" />    
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900