kumar
kumar

Reputation: 2944

Accessing array values from a Form in an ASP.NET MVC Controller

In the controlle,r I have this code:

var result = Request.Form[0];

where result has a value of 123,test,12,45,12/23/2010...etc..

How can I store each value in one variable?

Upvotes: 0

Views: 1248

Answers (3)

p.campbell
p.campbell

Reputation: 100567

It sounds like you're asking to have each of those values stored in a variable. It's not clear, as the question is written, what your end goal is.

Consider simply accessing those values by their array position.

string[] myValues = Request.Form[0]
                              .ToString()
                              .Split(',', StringSplitOptions.RemoveEmptyEntries);

foreach (string value in myValues)
{
    //do something

}

or

string customerID = myValues[0];
string customerName = myValues[1];

Upvotes: 1

Robaticus
Robaticus

Reputation: 23157

This feels like "broken as designed," but:

string result = (string)Request.Form[0];
string []results = result.Split(',');

Upvotes: 1

Vishal
Vishal

Reputation: 12369

I am not sure what you want to do maybe try this-

string str =Request.Form[0].Select(c=>c.FormFieldName).ToString();

ideally you should get values using the id -

       string valueforid=Request.Form["Id"].Tostring();

Upvotes: 1

Related Questions