3

I'm trying to implement a basic search page in the web app I'm developing. Right now the page looks like this

enter image description here

When a user enters a last name, the controller gets called to search the backend Microsoft SQL Server database for all accounts with that Last name

Right now the HTML form looks like this

@using (Html.BeginForm("SearchAct", "HomeController", FormMethod.Post))
{
    <form>
        <div>
            Last Name:<br>
            <input type="text" id="nameToFind">

            <input type="button" id="submitId" value="submit" />

        </div>
    </form>
}

It's supposed to call this controller

[HttpPost]
public void SearchAct()
{
    Console.WriteLine();
}

which will eventually execute the search and then put the results on the page. However, I can't get the controller to be called. I set a break point on the WriteLine so I know its never getting there and I don't know what I'm doing wrong

1
  • 1
    The could probably be FormMethod.Get rather than Post. You need a parameter in the method to bind to - say public ActionResult SearchAct(string searchText) and the input must have a matching name attribute - name="SearchText" (but you will find this a lot easier if you use a view model and strongly bind to it using @Html.TextBoxFor(m = m.SearchText). And you method needs to be ActionResult and return something. And assuming its HomeController and not HomeControllerController - then the 2nd paraeter of BeginForm() is "Home" Commented Nov 8, 2016 at 4:27

4 Answers 4

5

Add name attribute to your text box. Form collection build based on only name attributes.

Change button type to submit, then it will post your form to controller.

@using (Html.BeginForm("SearchAct", "Home", FormMethod.Post))
{

    <div>
        Last Name:<br>
        <input type="text" id="nameToFind" name="nameToFind">

        <input type="submit" id="submitId" value="submit" />

    </div>

}

@{
    if(ViewBag.SearchKey != null)
    {
        <span>
            Search Key: @ViewBag.SearchKey
        </span>
    }
}

Instead on Console.WriteLine() use ViewBag to send your required data back to view Refer below actions

//Get Action for rendering view
public ActionResult SearchAct()
{
    return View();
}


[HttpPost]
public ActionResult SearchAct(string nameToFind)
{
    ViewBag.SearchKey = nameToFind;

    return View();
}

Action parameter name and text box name attribute value must be same otherwise it will null

If your form contains multiple text box then read all input values form FromCollection or Request

[HttpPost]
public ActionResult SearchAct(FormCollection form)
{
    ViewBag.SearchKey = form["nameToFind"];

    return View();
}
Sign up to request clarification or add additional context in comments.

Comments

2

correct your form HomeController should be just Home

@using (Html.BeginForm("SearchAct", "Home", FormMethod.Post))
{
    <form>
        <div>
            Last Name:<br>
            <input type="text" id="nameToFind">

            <input type="button" id="submitId" value="submit" />

        </div>
    </form>
}

controller should accept the input parameter for your filter

[HttpPost]
public void SearchAct(string nameToFind)
{
    // filter snomething
    Console.WriteLine(nameToFind);
}

Comments

2

First: To create a form via using razor syntax:

@using (Html.BeginForm("SearchAct", "HomeController", FormMethod.Post))
{

}

That would generate a form like this:

<form method="post" action="/HomeController/SearchAct">

</form>

So, no need to create a nested form inside that.

@using (Html.BeginForm("SearchAct", "HomeController", FormMethod.Post))
{
    <form> // <- here
        <div>
            Last Name:<br>
            <input type="text" id="nameToFind">

            <input type="button" id="submitId" value="submit" />

        </div>
    </form> // <- here
}

Second: HomeController would match the controller what the full name is HomeControllerController.

So, if you want to hit Home controller, remove Controller from HomeController in @using (Html.BeginForm("SearchAct", "HomeController", FormMethod.Post)) {}

Thirth: if you want to catch nameToFind, you can try:

[HttpPost]
public void SearchAct(string nameToFind)
{
    Console.WriteLine();
}

Hope this help!

Comments

1

first of all we dont need to two form tag and you dont need to add controller suffix. remve that:

    @using (Html.BeginForm("SearchAct", "Home", FormMethod.Post))
 {
    <div>
        Last Name:<br>
        <input type="text" id="nameToFind" name="nameToFind">

        <input type="button" id="submitId" value="submit" />

    </div>
 }

Secondly if you need to search you need to add paramater same as the name of input type text :

    [HttpPost]
    public void SearchAct(string nameToFind)
    {
     Console.WriteLine();
    }

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.