dimanche 28 juin 2015

How to submit form and load view with results

How do I submit a form and loads the results from the request in a new view.

I have the following routes

    routes.MapRoute(
        name: "Default",
        url: "{controller}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

    routes.MapRoute(
        name: "GetResults",
        url: "{gueryString}",
        defaults: new { controller = "Result", action = "ShowResults", gueryString = "" }
    );

I have a index view with includes a partial view of my form I want to submit.

@Html.Partial("../Result/SearchForm")

The form itself is

  @using (Html.BeginForm("ShowResults", "Result", method: FormMethod.Post))
    {
        <form role="form">
            <div class="input-group input-group-lg">
                @Html.TextBoxFor(m => m.searchString, new { @class = "form-control" })
                <a href="javascript:$('form').submit();" class="input-group-addon">
                    <i class="fa fa-search"></i>
                </a>
            </div>
        </form>
    }

The new action itself in class ResultController

 public ActionResult ShowResults(SearchSubmitModel model)
    {

        RequestExternalComparisonData.ValidateSearchString(model);
        ViewBag.GameComparisonList = RequestExternalComparisonData.DoSearch(model.searchString);
        return View();
    }

The problem is the default index action is always ran on the form submit unless I completely remove the default route, but I need to initially load the index with my form. How do I trigger my new action on the form submit?

Aucun commentaire:

Enregistrer un commentaire