简体   繁体   中英

How do I redirect to the next page after pressing the submit button?

I am creating a site with ASP.NET MVC 4 and C#. The main page is a search bar with a submit button with the label "search". I want the user to be able to enter a musical artist and press the "search" button. The site should then go to a "results" page with the results.

I am having trouble getting to the results page after pressing the search button.

My view Index.cshtml form looks like this:

<form method="post" class="form-inline">
    <div class="row">
        <div class="col-md-1" style="min-width:300px">
            <input type="text" name="Artist" class="form-control box-shadow--4dp" placeholder="Artist Name" />
        </div>
        <div class="col-md-2">
            <input type="submit" class="btn btn-default box-shadow--4dp" value="Search" />
        </div>
    </div>
</form>

And my HomeController.cs looks like this:

namespace Task2.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        // edit
        [HttpPost]
        public ActionResult Index(string Artist)
        {
            return RedirectToAction("Result", "Home");
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "For more information you can contact me using the form below.";

            return View();
        }

        public ActionResult Result(string Artist)
        {
            ViewBag.Message = Artist;
            return View();
        }
    }
}

So to summarize, how can I cause the submit button to redirect the user to the Results page with the results?

The way your HTML is currently set up, your form would POST to the Index method of your controller.

To do a redirect you could do:

[HttpPost]
public ActionResult Index(string searchTerm)
{
    //handle your search stuff here...
    return RedirectToAction("Results", "Home");
}

However, you can wrap your current form in an HTML.BeginForm and post to a specific Action Method other than the Index method -- perhaps to a search action method which would then redirect to Results, passing in the results collection for instance.

If you want your input as a query string then simply change your form method to GET . It will redirect you to Result Page with input as querystring.

  @using (Html.BeginForm("Result","Home",FormMethod.Get,new {@class= "form-inline" }))
{
    <div class="row">
        <div class="col-md-1" style="min-width:300px">
            <input type="text" name="Artist" class="form-control box-shadow--4dp" placeholder="Artist Name" />
        </div>
        <div class="col-md-2">
            <input type="submit" class="btn btn-default box-shadow--4dp" value="Search" />
        </div>
    </div>
}

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