1

Hi i have no idea with creating event textchanged or somethink as this for textBox. It will be doing some form of find when text changed.

Image with code

@{
    ViewBag.Title = "Index";
    <br />
    using (Html.BeginForm())
    {
        <p>
        <p>
            @Html.TextBox("searchString")
        <input type="submit" value="Find" />
    </p>
    </p>
    }
}

<h2>Index</h2>

I want it call this (This is written right):

public ActionResult Index(string searchString)
    {
        if (String.IsNullOrWhiteSpace(searchString))
        {
            return View(db.Regions.ToList());
        }
        else
        {
            List<Regions> collectionOfRegions = db.Regions.ToList();
            return View(collectionOfRegions.Where(x => x.MatchBetweenAllFields(searchString)));
        }
    }

1 Answer 1

1

Replace TextBox

@Html.TextBox("YourTextBox", new { onchange="yourForm.submit();"})

And add JavaScript

<script type="text/javascript">
        $(function () {
            $('#YourTextBox').change(function () {
              //Content to send 
              var yourText = $(this).val();

              //Post the content of your Textbox to your "YourAction" action in "YourController"
              $.post('@Url.Action("YourAction","YourController")', { "YourText" : yourText }, function(data){
                  //Do something with the results here
                  alert(data);
              });
            });
        });
</script>

Also edit controller

[HttpPost]
public void YourAction(string yourText)
{
      if (String.IsNullOrWhiteSpace(yourText))
    {
        return View(db.Regions.ToList());
    }
    else
    {
        List<Regions> collectionOfRegions = db.Regions.ToList();
        return View(collectionOfRegions.Where(x => x.MatchBetweenAllFields(yourText)));
    }
}
Sign up to request clarification or add additional context in comments.

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.