Home Ask Login Register

Developers Planet

Your answer is one click away!

tamrezh21 February 2016

Enable search from home controller to another view using ASP.NET MVC

I am trying to write a code which will get the city-name from a list and then according to the name it will redirect to the url search.But this code is not working properly. At first I get all city name a┬┤from a text file and then show it to text-box in view.

My auto-complete code and actionresult search is-

  public class HomeController : Controller
   public ActionResult Index()
      return View();
     public ActionResult Autocomplete(string term)
        var Cityname = System.IO.File.ReadAllLines(Server.MapPath(@"~/App_Data/City.txt"));
        return this.Json(Cityname, JsonRequestBehavior.AllowGet);

   public ActionResult Search()
     return View();

Now in my Index.cshtml I make a search form where I get the name of all city-

  <div class="search-form">
     <form action="index.html" method="get">
         <div class="input-group">

         <input type="text" placeholder="Please enter City Name" name="Cityname" id="Cityname" class="form-control input-lg">
        <div class="input-group-btn">
            <button class="btn btn-lg btn-primary" type="submit">
                <a href="@Url.Action("Search","Home","Cityname")">Search</a>



Now I want to search for next page suppose Home/Search/Cityname. How can I do that?


Chris Pratt February 2016

You can't without JavaScript, and any solution that relies on JavaScript completely to function is dead on arrival.

Submitting the form will send the search parameters as either part of the query string if using GET (/Home/Search?Cityname=Berlin) or as part of the post body if using POST. Either way, the form action, the URL you're submitting to (/Home/Search), is constant. As I said, you could use JavaScript to change the form action whenever your Cityname input is changed, but that would be an extremely brittle solution.

Post Status

Asked in February 2016
Viewed 2,869 times
Voted 11
Answered 1 times


Leave an answer

Quote of the day: live life