views:

891

answers:

2

What is the best way to pass random data from a view to a controller.

I have a number of views that are identical except for a few pieces of state information. instead of creating a page for each webpage, I want to have one page and I just pass over these variables dynamically.

A: 

Although not the "recommended" approach, you can make your action method accept a FormCollection as the parameter. You can then write your own logic to pull whatever data you need from that object. Basically the FormCollection will contain all fields inside the form that is posted as a key-value pair.

The signature would look like this:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection form)
{
  // logic here to 
}
Ash M
if that is not recommended, then what would be the alternative?
ooo
AFAIK, that is the only way to pass generic parameters to a controller. The recommended approach is to create a controller for each page. Just bite the bullet and do that if you want the recommended way. At the end of the day, it is your application to create and maintain, and you have the final say on the solution you choose - all we can do is offer you potential solutions.
Ash M
+1  A: 

Not shure about the "random data" - but maybe the question is realy about "views that are identical"

Put the common parts into a partial and the differing parts into the page, if your layout allows it.

You would have a couple of pages then, but no duplicate code.

Or is your problem more on the controller/model side?

Malcolm Frexner