leora
leora

Reputation: 196429

how to pass data from view to controller in asp.net mvc

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.

Upvotes: 2

Views: 5245

Answers (2)

Mathias F
Mathias F

Reputation: 15891

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?

Upvotes: 1

Ash M
Ash M

Reputation: 1399

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 
}

Upvotes: 1

Related Questions