I have form collection accessed in action method ,but how to get the value of it .I tried like this
string value = collection[1];
but I am not getting the value .How can I access the value in action method.
There are a number of ways in which you can pass parameters to action methods in ASP.NET Core MVC. You can pass them via a URL, a query string, a request header, a request body, or even a form. This article talks about all of these ways, and illustrates them with code examples.
FormCollection is one way of retrieving view data in controller. Depending on the type of value in input, you can parse its non-string value to string in the Action method.
FormCollection collection = new FormCollection(); collection. Add("Name", "Value");
If you have:
<input type="text" name="inputName" />
You could use the attribute name of the element like below:
[HttpPost]
public ActionResult yourAction(FormCollection collection)
{
string value = Convert.ToString(collection["inputName"]);
...
return View();
}
I think you should attempt to stear clear of the formcollection object if you are able to, in favour of a strongly typed viewmodel. there are a few examples here on SO and i've linked the 1st one that I searched for:
passing FormCollection to controller via JQuery Post method and getting data back...
however, if you're keen to tie yourself in knots :), then here's an example iterating thro a formcollection:
http://stack247.wordpress.com/2011/03/20/iterate-through-system-web-mvc-formcollection/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With