gpt4 book ai didi

ajax - MVC3 Ajax.BeginForm 禁用 JavaScript

转载 作者:行者123 更新时间:2023-12-02 20:18:17 25 4
gpt4 key购买 nike

我在没有启用 JavaScript 的情况下让表单正常工作时遇到问题。

这应该足以继续下去,询问您是否需要了解其他信息 - 我不想只是将整个解决方案放在这里!

~/Views/_ViewStart.cshtml:

@{ Layout = "~/Views/Shared/Layout.cshtml"; }

~/Views/Shared/Layout.cshtml:

@using System.Globalization; @{ CultureInfo culture = CultureInfo.GetCultureInfo(UICulture); }<!DOCTYPE html>
<html lang="@culture.Name" dir="@(culture.TextInfo.IsRightToLeft ? "rtl" : "ltr")">
<head>
<title>AppName :: @ViewBag.Title</title>
<link href="@Url.Content("~/favicon.ico")" rel="shortcut icon" type="image/x-icon" />
<link href="@Url.Content("~/apple-touch-icon.png")" rel="apple-touch-icon" />
<link href="@Url.Content("~/Content/css/site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Content/js/jquery-1.6.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Content/js/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Content/js/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Content/js/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Content/js/app.js")" type="text/javascript"></script>
@RenderSection("SectionHead", false)
</head>
<body>
<div id="page-container">
<div id="nav">
<div id="nav-user">
@{ Html.RenderAction("LoginStatus", "Account"); }
@{ Html.RenderPartial("CultureSelector"); }
</div>
</div>
<div id="page-content">
<h2>@ViewBag.Title</h2>
@RenderBody()
</div>
</div>
</body>
</html>

~/Views/Account/Index.cshtml:

@model AccountFilterModel
@{
ViewBag.Title = "Account Home";
var loadingId = "loading" + new Random().Next();
Model.FilterFormId = "filter-account-form";
}
@using (Ajax.BeginForm("List", "Account", Model, new AjaxOptions { UpdateTargetId = "result-list", LoadingElementId = loadingId }, new { id = "filter-account-form" })) {
<!-- form controls and validation summary stuff -->
<input id="filter" type="submit" value="Filter" />
<span id="@loadingId" style="display: none">
<img src="@Url.Content("~/Content/images/ajax-loader.gif")" alt="Loading..." />
</span>
}
<div id="result-list">
@{ Html.RenderAction("List", Model); }
</div>

~/Views/Account/List.cshtml:

@model FilterResultModel
@helper SortLink(AccountSort sort, SortDirection dir) {
string display = (dir == SortDirection.Ascending ? "a" : "d"); // TODO: css here
if (Model.Filter.SortBy != null && ((AccountSortModel)Model.Filter.SortBy).Sort == sort && dir == Model.Filter.SortOrder) {
@:@display
} else {
FilterModel fm = new FilterModel(Model.Filter);
fm.SortBy = AccountSortModel.SortOption[sort];
fm.SortOrder = dir;
<a href="@Url.Action("Index", "Account", fm.GetRouteValueDictionary())" onclick="@(string.Format("setFormValue('{0}', '{1}', '{2}'); setFormValue('{0}', '{3}', '{4}'); formSubmit('{0}'); return false;", Model.Filter.FilterFormId, Html.PropertyNameFor(x => x.Filter.SortOrder), dir, "AccountSort", sort))">@display</a>
}
}

@if (Model.Results.Count > 0) {
var first = Model.Results.First();
<table>
<caption>
@string.Format(LocalText.FilterStats, Model.FirstResultIndex + 1, Model.LastResultIndex + 1, Model.CurrentPageIndex + 1, Model.LastPageIndex + 1, Model.FilteredCount, Model.TotalCount)
</caption>
<thead>
<tr>
<th>
@Html.LabelFor(m => first.Username)
<span class="sort-ascending">
@SortLink(AccountSort.UsernameLower, SortDirection.Ascending)
</span>
<span class="sort-descending">
@SortLink(AccountSort.UsernameLower, SortDirection.Descending)
</span>
</th>
<!-- other table headers -->
</tr>
</thead>
<tbody>
@foreach (AccountModel account in Model.Results) {
<tr>
<td>@Html.EncodedReplace(account.Username, Model.Filter.Search, "<span class=\"filter-match\">{0}</span>")</td>
<!-- other columns -->
</tr>
}
</tbody>
</table>
Html.RenderPartial("ListPager", Model);
} else {
<p>No Results</p>
}

AccountController.cs的相关部分:

public ActionResult Index(AccountSort? accountSort, FilterModel model = null) {
FilterModel fm = model ?? new FilterModel();
if (accountSort.HasValue) fm.SortBy = AccountSortModel.SortOption[accountSort.Value];
return View(fm);
}

public ActionResult List(AccountSort? accountSort, FilterModel model = null) {
FilterModel fm = model ?? new FilterModel();
if (accountSort.HasValue) fm.SortBy = AccountSortModel.SortOption[accountSort.Value];
return Request.IsAjaxRequest() ? (ActionResult)PartialView("List", Service.Get(fm)) : View("Index", model);
}

启用 JavaScript 后,效果很好 - div#result-list 的内容按预期更新。

如果我不执行 Request.AjaxRequest() 并仅返回 PartialView,那么在禁用 javascript 的情况下,我会得到一个仅包含结果内容的页面在上面。如果我有上面的代码,那么我会得到一个 StackOverflowException

如何让它发挥作用?

解决方案

感谢@xixonia,我发现了这个问题 - 这是我的解决方案:

public ActionResult List(AccountSort? accountSort, FilterModel model = null) {
FilterModel fm = model ?? new FilterModel();
if (accountSort.HasValue)
fm.SortBy = AccountSortModel.SortOption[accountSort.Value];
if (Request.HttpMethod == "GET")
return PartialView("List", Service.Get(fm));
if (Request.HttpMethod == "POST")
return Request.IsAjaxRequest() ? (ActionResult) PartialView("List", Service.Get(fm)) : RedirectToAction("Index", model);
return new HttpStatusCodeResult((int) HttpStatusCode.MethodNotAllowed);
}

最佳答案

可以使用下面的扩展方法来判断请求是否是ajax请求

Request.IsAjaxRequest()

如果是,则可以返回部分 View ,否则可以返回完整 View 或重定向。

if(Request.IsAjaxRequest())
{
return PartialView("view", model);
}
else
{
return View(model);
}

编辑:问题是:

当请求不是 AJAX 请求时,“列表”返回“索引” View :

public ActionResult List(AccountSort? accountSort, FilterModel model = null) {
FilterModel fm = model ?? new FilterModel();
if (accountSort.HasValue) fm.SortBy = AccountSortModel.SortOption[accountSort.Value];
return Request.IsAjaxRequest() ? (ActionResult)PartialView("List", Service.Get(fm)) : View("Index", model);
}

“索引” View 正在呈现“列表”操作:

@{ Html.RenderAction("List", Model); }

又名:递归。

您需要设计一种在不绘制索引页的情况下显示列表的方法,或者使索引页以列表模式作为参数绘制部分 View 。

关于ajax - MVC3 Ajax.BeginForm 禁用 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5932333/

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