tags:

views:

34

answers:

3

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://url/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

A: 

Chaining ASP.NET MVC actions
http://jwbs-blog.blogspot.com/2009/08/chaining-aspnet-mvc-actions.html

Robert Harvey
A: 

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" />

<% } %>
Omu
A: 

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.

Robert Harvey