0

I'm having some trouble and not sure what's going on.

I have a form with input value and want to be able to get that input value and send it back to my controller (server side).

My html code

<form action="/Home/Search" method="get">
                    <button class="search-btn-widget"></button>
                    <input class="search-field" id="sub" type="text" onblur="if(this.value=='')this.value='Search';" onfocus="if(this.value=='Search')this.value='';" value="Search" />
                </form>

Then in my controller I have

string sub = Request["sub"];

However it ends up being null and not sure what's going on. Any ideas?

Claud
  • 1,065
  • 3
  • 26
  • 38

4 Answers4

2

Just to make it work: add the name attribute

 <input class="search-field" id="sub" name="sub" ...

but check this.

Community
  • 1
  • 1
1

You need to add the name attribute to the input tag.

If you pull up the developer console and take a look at the HTTP GET request that is being sent, you will see that no query string is being associated with the request. This will let you know that the issue on the HTML side and not the ASP.Net MVC side.

Update input tag:

<input class="search-field" id="sub" name="sub" type="text" onblur="if(this.value=='')this.value='Search';" onfocus="if(this.value=='Search')this.value='';" value="Search" />

Update Controller Action to:

public ActionResult Search(string sub)
SBurris
  • 7,378
  • 5
  • 28
  • 36
1

1) If you wanna see your input into the Request you must send your Form as POST:

<form action="/Home/Search" method="POST">

2) Make sure that input has a name:

<input class="search-field" id="sub" name="name" 
 type="text" 
 onblur="if(this.value=='')this.value='Search';"
 onfocus="if(this.value=='Search')this.value='';" 
 value="Search" />

Then you will be able so see It in the request

Fals
  • 6,813
  • 4
  • 23
  • 43
  • I wanted to see it in the GET but the name attribute was the problem. But thanks and +1. – Claud Nov 28 '13 at 21:35
0

You should add the name attribute to the input element.

Yosi Dahari
  • 6,794
  • 5
  • 24
  • 44