简体   繁体   中英

How to make event on textbox.text_changed (ASP.NET)

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)));
        }
    }

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)));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM