0

I have a textbox like below

   @using (Html.BeginForm("checkUserType", "Place", new {type =  }))
    {
        <div id="login">
            <div class="userNameLabel">UserName</div>
            <div class="userNameField"><input type="text" id="txtUserName"/><span><input type="button" value="ok" id="ok" /></span></div>
        </div>
    }

I want to pass the textbox value to my controller. For that I used the below code, but it's not working... Pls help...

Action method

[HttpPost]
        public ActionResult checkUserType(string type)
        {
            var elements = from element in db.USERS
                           where element.UserType == type
                           select element;
            if (elements == null)
            {
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Place/Index");
            }
        }
6
  • Please show your action method, also if you are going for GET request than your code needs to be changed at least like this window.location.href = '@Url.Action( "checkUserType", "Place" )' + '?type=' + type; or like this if the type parameter is being taken from the route window.location.href = '@Url.Action( "checkUserType", "Place" )' + '/' + type; Commented Jul 5, 2013 at 9:25
  • @tpeczek- I edited my code.Hope you can see it. Commented Jul 5, 2013 at 9:29
  • Then you should go for the Form in order to have POST request (you should look at @NeerajDubey answer). Commented Jul 5, 2013 at 9:32
  • @tpeczek-By doing like Neeraj said. I got a problem Commented Jul 5, 2013 at 10:23
  • What kind of problem? Commented Jul 5, 2013 at 10:29

4 Answers 4

5

Try this once

window.location.href = '@Url.Action( "checkUserType", "Place" )?type='+type
Sign up to request clarification or add additional context in comments.

Comments

0

Try This

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<% Html.BeginForm("Search", "Employee");%>
<table>
   <tr>
    <td>
        Name:
    </td>
    <td>
        <input type="text" id="SearchText" name="SearchText" style="width: 325px" />
    </td>
    <td> 
    </td>
    <td>
        <input type="submit" id="Search" value="Search" />
    </td>
</tr>
</table>
<% Html.EndForm();%>

your action goes like this...

public ActionResult Search(string searchText)
{ 
 }

Hope it helps you

Comments

0

MVC will map your names of inputs to parameters of your method.

However, your current input doesn't specify a name:

<input type="text" id="txtUserName"/>

So add that:

<input type="text" name="type" id="txtUserName"/>

And it will map to the parameter. And take away the anonymous object from the BeginForm since that value will be provided by the input field, so doen't need to be in the form's action.

Comments

0

I just update your code try this

 @using (Html.BeginForm("checkUserType", "Place"))
    {
        <div id="login">
            <div class="userNameLabel">
                UserName</div>
            <div class="userNameField">
                <input type="text" id="txtUserName" name="UserName" /><span><input type="submit" value="ok" id="ok" /></span></div>
        </div>
    }

and controller

 public ActionResult checkUserType(string UserName)
        {
 string _data = UserName;


        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.