michael
michael

Reputation: 585

How to access my formcollection in action method ASP.NET mvc ?

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.

Upvotes: 17

Views: 67193

Answers (7)

Ishwor Khadka
Ishwor Khadka

Reputation: 33

See your HTML file (or use Inspect Element) to see the name attribute in the input field. For instance if you have

<input type = "text" name = "YourName"/>

In the controller,

[HttpPost]
public ActionResult ActionName(FormCollection fc)     /*Using FormCollection*/
        {
                string variable_name = fc["YourName"];
                return View();
        }

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.

Upvotes: 0

Marty A
Marty A

Reputation: 533

My preferred option is to use UpdateModel.

Instead of manually mapping the fields, this MVC method will automatically bind the properties from the data available in the request, the same way as if you had passed a strict type in as a parameter to the action.

[HttpPost]
public ActionResult FormCollectionEg()
{
    var model = new Username();

    UpdateModel<Username>(model);

    return View(model);
}

The above code will include data from the QueryString aswell as the form data which may not be suitable. If you were using a parameter in the action, you'd restrict this by using [FromBody], but with UpdateModel you can still achieve the same thing by passing in the FormCollection as the value provider.

[HttpPost]
public ActionResult FormCollectionEg(FormCollection collection)
{
    var model = new Username();

    UpdateModel<Username>(model, collection);

    return View(model);
}

Upvotes: 0

Hemlata Gehlot
Hemlata Gehlot

Reputation: 361

Please try this example,hope it will help ...

 public class UserName
    {        
       public string FName { get; set; }
       public string LName{ get; set; }               
    }


        [HttpGet]
        public ActionResult FormCollectionEg()
        {
            return View();
        }

        [HttpPost]
        public ActionResult FormCollectionEg(FormCollection data)
        {
            UserName UserObj = new UserName();
            UserObj.FName = data["fname_name"];
            UserObj.LName = data["lname_name"];
            return  RedirectToAction("DisplayFormCollectionData", UserObj);           
        }


        public ActionResult DisplayFormCollectionData(UserName reg)
        {
             return View(reg);
        }

Create two view - DisplayFormCollectionData FormCollectionEg

DisplayFormCollectionData

@model YourProjectNamespace.Models.UserName

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>DisplayFormCollectionData</title>
</head>
<body>
    <div>
        <h4>User Deatails</h4>
        <hr />
        <dl class="dl-horizontal">
            <dt>
                @*@Html.DisplayNameFor(model => model.FName)*@
                First Name....
            </dt>

            <dd>
                @Html.DisplayFor(model => model.FName)
            </dd>

            <dt>
                @*@Html.DisplayNameFor(model => model.LName)*@
                Last Name...
            </dt>

            <dd>

                @Html.DisplayFor(model => model.LName)
            </dd>

        </dl>
    </div>
    <p>
        @*@Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
        @Html.ActionLink("Back to List", "Index")*@
    </p>
</body>
</html>

FormCollectionEg-

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>FormCollectionEg</title>
</head>
<body>
    @using (Html.BeginForm("FormCollectionEg", "Home"))
    {
        <table>
            <tr>
                <td>Enter First Name</td>
                <td><input type="text" id="fname_id" name="fname_name" /></td>
            </tr>
            <tr>
                <td>Enter Last Name</td>
                <td><input type="text" id="lname_id" name="lname_name" /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" /></td>
            </tr>
        </table>



    }

</body>
</html>

Upvotes: 0

German Blanco
German Blanco

Reputation: 816

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();
}    

Upvotes: 33

jim tollan
jim tollan

Reputation: 22485

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/

Upvotes: 2

Praveen Prasad
Praveen Prasad

Reputation: 32107

Create a view like this

<form action="/myController/myAction" method="post">
User Name <input type="text" name="userName" /> <br>
Country <input type="text" name="country" /><br>
<input type="submit" value="submit" />
</form>

Create action like below

public ActionResult myAction(string userName, string country){
      //do some thing with userName
      //asp.net mvc3 has automatically bind that for you
}

Note: Above written code is not a recommended way of doing things, its just for a demo.

Upvotes: -1

ipr101
ipr101

Reputation: 24236

Something like (code not tested) -

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddNewLink(FormCollection collection)
{
    string url = collection[1].ToString();
}

Upvotes: 1

Related Questions