gpt4 book ai didi

c# - MVC 局部 View 问题

转载 作者:太空狗 更新时间:2023-10-29 23:21:02 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com