通过控制器方法返回局部视图

时间:2016-05-13 09:38:20

标签: c# asp.net-mvc razor partial-views

我有一个我想要返回的搜索部分视图,但我希望通过在Controller中通过现有的部分视图结果运行它而不是直接加载视图来返回它。

所以,在控制器中我有:

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

在搜索视图中我有:

<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>

当我尝试运行代码时,它告诉我它无法找到_GetSearch视图,这在技术上是正确的,但我不是在寻找一个视图我正在寻找控制器中的方法。

0 个答案:

没有答案