0

I have a Search Partial View that I want to return but I want to return it by running it through an exisiting Partial View Result in the Controller instead of loading the view directly.

So, in the controller I have:

public ActionResult Index()
        {
            return View();
        }

        public PartialViewResult _GetSearch(List<Search> model)
        {

            return PartialView("_Search", model);
        }

        [ValidateAntiForgeryToken()]
        public PartialViewResult _BeginSearch(string search)
        {
            var results = SearchModels(search).ToList();

            return PartialView("_GetSearch", results);
        }

And in the search view itself I have:

<div class="col-md-4">
    <div id="modelSearch" class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title"><i class="fa fa-search"></i> Search by Model / Manufacturer</h3>
        </div>
        <div class="panel-body">
            @using (Ajax.BeginForm("_BeginSearch", "Home", new AjaxOptions() { UpdateTargetId = "modelSearch" }))
            {
                @Html.AntiForgeryToken()
                <div class="input-group">
                    @Html.TextBox("search", null, new {id = "name", @class = "form-control", placeholder = "Please enter a manufacturer or model"})
                    <span class="input-group-btn">
                        <button id="search" class="btn btn-default" type="submit"><i class="fa fa-search"></i></button>
                    </span>
                </div>
                if (Model != null)
                 {
                     <div class="searchResults fade">
                         @foreach (var s in Model)
                         {
                         <div class="result">
                             @switch (s.ResultType)
                             {
                                 case "Man":
                                     <a href="#">@s.Manufacturer</a>
                                     break;
                                 case "Mod":
                                     <a href="#">@s.Manufacturer @s.Model</a>
                                     <img src="~/Images/General/(@s.TierId).png" alt="Tier @s.TierId"/>
                                     break;
                             }
                         </div>
                         }
                     </div>
                 }
            }
        </div>
    </div>
</div>

When I try and run the code it tell me that it cannot find the _GetSearch view, which yes technically is right, but I'm not looking for a view I'm looking for a method in the controller.

Web Develop Wolf
  • 5,996
  • 12
  • 52
  • 101
  • Pls explain your scenario better. what do you need. its unclear. – Karthik M R May 13 '16 at 09:56
  • your question is unclear...please use some scenario to make it understood – stylishCoder May 13 '16 at 09:57
  • 1
    It is unclear what you're asking; but I am going to take a punt at "instead of loading the View directly" (which I guess means you "redirected" to _only_ your partial's data. Ensure that you include/use `jquery-1.x.x.js` and `jquery.unobtrusive-ajax.js` at the top of your main View page; this will allow the page to load in the Partial data without redirecting you. Good example here: http://stackoverflow.com/questions/5410055/using-ajax-beginform-with-asp-net-mvc-3-razor – Geoff James May 13 '16 at 10:04
  • I was missing the unobtrusive Ajax script - thanks – Web Develop Wolf May 13 '16 at 10:28

0 Answers0