c# - MVC 局部 View 问题

标签 c# ajax asp.net-mvc asp.net-mvc-4

我是 MVC 的新手,我想知道是否有更好的方法来做到这一点。我有一个文本框供用户输入他们的搜索,然后根据该搜索我在所述搜索框下方显示一些结果。我试图避免在我看来有太多代码逻辑,并且想知道是否有更好的方法来处理这个问题。这是我现有的代码,如果我的其余逻辑通过,它将根据“Model.Results”的值返回 3 个局部 View 之一或一个按钮:

@section CustomerPrefixInfo
{
    @if (Model.Results == PrefixSearch.SearchResults.CustomerFound)
    {
        @Html.Partial("_CustomerPrefixInfo")
    }
    @if (Model.Results == PrefixSearch.SearchResults.PrefixResultsFound)
    {
        @Html.Partial("_PrefixResults")
    }
    @if (Model.Results == PrefixSearch.SearchResults.AnimalsFound)
    {
        @Html.Partial("_AnimalSearchResults")
    }
    @if (Model.Results == PrefixSearch.SearchResults.ValidNewPrefix)
    {
        using (Html.BeginForm("Index", "PrefixManagement", new { prefix = Model.AnimalPrefix.Prefix, dbPrefix = Model.AnimalPrefix.DbPrefix }))
        {
            <fieldset>
                <input id="btnReservePrefix" type="submit" value="Reserve Prefix" />
            </fieldset>
        }
    }
}

我想将它放在一个 Controller 中,以便它只返回要显示的 View ,然后只在页面上显示该 View 。在做了一些研究之后,我认为使用 Ajax.BeginForm 并将 InsertionMode 设置为 InsertAfter 可以解决问题:

@using (Ajax.BeginForm("GenericSearch", "Home", FormMethod.Post, new AjaxOptions { InsertionMode = InsertionMode.InsertAfter, UpdateTargetId = "searchResults" }))
{
    <fieldset>
        <input id="btnPrefixSearch" type="submit" value="Prefix Search/Validate"/>
        @Html.EditorFor(model => model.Input)
    </fieldset>
    <div id="searchResults">

    </div>
}

我的 GenericSearch Action 然后使用一个开关来决定返回哪个局部 View :

public ActionResult GenericSearch(PrefixSearch prefixSearch)
{
    //some database logic here to get the results
    switch (prefixSearch.Results)
    {
        case PrefixSearch.SearchResults.CustomerFound:
            return PartialView("_CustomerPrefixInfo", prefixSearch);
        case PrefixSearch.SearchResults.PrefixResultsFound:
            return PartialView("_PrefixResults", prefixSearch);
        case PrefixSearch.SearchResults.AnimalsFound:
            return PartialView("_AnimalSearchResults", prefixSearch);
        default:
            return null;
     }
}

但是当我尝试这样做时,它会将部分 View 放在新页面上。

这是我的部分观点之一(他们都 3 大部分与此相同)

@model MVC_Test_Project.Models.PrefixSearch

@{
    ViewBag.Title = "PrefixResults";
 }

@{
    Layout = null;
 }
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.PrefixResults[0].Prefix)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PrefixResults[0].CustomerCount)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PrefixResults[0].Link)
        </th>
    </tr>

@foreach (var item in Model.PrefixResults)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Prefix)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CustomerCount)
        </td>
        <td>
            <a href="@item.Link">edit</a>               
        </td>
    </tr>
}
 </table>

如有任何帮助,我们将不胜感激!

编辑 这只是一个有用的提示,以防万一有人犯了和我一样的愚蠢错误,确保你的包在你的脚本之前被调用。

@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrap")
<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.min.js"></script>

当提到要使用它们时,我在最后两行中添加了这些内容,但它们在我的包之上....因此 ajax 无法正常工作。感谢大家的帮助,现在一切都很好! --约瑟夫

最佳答案

I would like to put this inside a controller so that it just returns the view

看起来很简单:

View 模型:

public class MyModel
{
  public string PageToRender { get; set; }
}

Controller Action

public ActionResult DoLogic()
{
  //var Results = ?? as The ENum

  switch(Results)
  {
    PrefixSearch.SearchResults.CustomerFound:
      model.PageToRender = "_CustomerPrefixInfo";
    // etc etc
  }    

  return View(model);
}

查看:

 @{if (!string.IsNullOrEmpty(model.PageToRender))
   Html.RenderPartial(model.PageToRender);}

虽然我可能会装饰枚举:

public enum SearchResults
{
  [Display(Name="_CustomerPrefixInfo")]
  CustomerFound
}

然后没有switch语句,你只是grab the enum value display attribute's name value .

关于c# - MVC 局部 View 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37171541/

相关文章:

javascript - 使用 jQuery 获取父级下的子级

php - MySQL Jquery 更新数组?

c# - 系统.Data.SqlClient.SqlException : Invalid column name 'GenreId'

c# - MVC 中的动态模型(非类)元数据提供程序

javascript - 单击页面上的任意位置时切换 div 内容

c# - 如何使用 Blazor 创建表单向导?

c# - 打开 TLS 1.0、TLS 1.1、TLS 1.2 ... Asp.NET IIS 10.0

c# - 将大型 C# winforms 应用程序升级到 WPF 的最简单方法是什么

c# - 我如何计算 Hot observables?

jquery - 缓存页面 AJAX 不起作用