neel
neel

Reputation: 5293

Form Collection issue

I have an html begin form in mvc

@using (Html.BeginForm("Search", "Reports",FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-inline"  }))
{ 
   <div class="form-group">
        <input type="text" class="form-control input-sm" placeholder="Value" name="SearchValue">
        <input type="text" class="form-control input-sm second-value" placeholder="Value" style="display:none;" name="SearchValue1">
        <button type="button" class="btn btn-default btn-Add">+</button>

    </div>
    <div id="othersearch"></div>
    <input type="submit" value="Search" class="btn btn-primary" />
}

I want to post this form item in one controller

public ActionResult Search(FormCollection collection)
{
    string searchvalue = collection.Get("SearchValue");
    return View();
}

my problem is that some times second text box is not visible.. that time i dont want to collect the values.And when i press button add generate the same type of input field in the form with same name (i can add many input box). Then how can i collect all these in my controller . please help me..

Upvotes: 0

Views: 1415

Answers (3)

Rajshekar Reddy
Rajshekar Reddy

Reputation: 18987

When ever you add a element dynamically make sure you also set it a name. so when you add a new input element it must be

 <input type="text" name="NewTextBox" class="form-control input-sm" placeholder="Value" name="searchvalue">

So like this no matter how many text boxes you add, All will have same name. Once you post the form. In your controller do this.

[HTTPPOST]
public ActionResult Search(MyModel newModel,string[] NewTextBox)
{
 // here as you had dynamic textbox with name = NewTextBox you 
 //will get all its value binded to the above string[]

}

OR

You can retrive them using Request.form["NewTextBox"] as

[HTTPPOST]
public ActionResult Search(MyModel newModel) 
{ 
 var values = Request.Form[NewTextBox];    
}

But I would recommend you the first approach where you use the MVC Model Binder to take care of all the things. You will just have array of values to play with.

NOTE: Always make sure you get the names right, and use the name right while playing with MVC. As all the binding depends on the naming itself.

Upvotes: 0

RKT
RKT

Reputation: 114

You can get value of all textboxes having same name using following code:

    var results = ((String[])formcollection.GetValue("mytxt").RawValue).ToList();
            foreach (var item in results)
            {
                //string name = item;
            }

Upvotes: 0

Puneet Khurana
Puneet Khurana

Reputation: 396

You can have all the text boxes with same name "SeachValue" in your case.

string searchvalue = collection.Get("SearchValue");

This will return all text box values as comma saperated string which you can split and use further.

Check out the screen shot enter image description here

the html

enter image description here

and the results

enter image description here

Upvotes: 5

Related Questions