maX
maX

Reputation: 742

asp.net mvc set action explicitly

I have 2 views for a input operation in my application.

The first view (lets call it view1) submits a form. Based on the form some operations on database is done and second view(View2) is returned with some other data from the database as a model.

controller action code :

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult View1(FormCollection fc)
{
   //database ops to fill vm properties    
   View2ViewModel vm=new View2ViewModel();

   return View("View2", vm);
}

Now, since I return a new view and not a action redirect the url is still http://www.url.com/View1 but everything works as it is supposed to.

The problem:

When I submit the form in View2 it calls the View1 action method, not the View2 action method. Probably because the url is still View1.

What can I do to call the action View2

Upvotes: 1

Views: 638

Answers (2)

Robert Harvey
Robert Harvey

Reputation: 180788

Your controller methods should look something like this:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult View1(int id)
{
   //database ops to fill vm properties    
   View1ViewModel vm=new View1ViewModel(id);

   return View("View1", vm);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult View1(FormCollection fc)
{
   // Code here to do something with FormCollection, and get id for
   // View2 database lookup

   return RedirectToAction("View2", id);
}

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult View2(int id)
{
   // Do something with FormCollection 

   return View("View2", vm);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult View2(int id)
{
   // Do something with FormCollection 

   return RedirectToAction("View3", id);
}

...and so on.

Upvotes: 1

Omu
Omu

Reputation: 71208

You can be in any View you want and submit the form to any controller's action from your application, cuz you can specify that

<% using (Html.BeginForm("ThAction", "Controller"))
   { %>

   Enter your name: <%= Html.TextBox("name") %>
   <input type="submit" value="Submit" />

<% } %>

Upvotes: 0

Related Questions