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.

2
  • 2
    Do you mean... from your controller to your view? Commented Aug 4, 2009 at 6:13
  • 1
    Could you give us more info on the html and actions you're talking about...? Commented Aug 4, 2009 at 6:55

2 Answers 2

1

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 
}
Sign up to request clarification or add additional context in comments.

2 Comments

if that is not recommended, then what would be the alternative?
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.
1

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.