部分视图不使用异步请求进行更新

编程入门 行业动态 更新时间:2024-10-25 12:26:34
本文介绍了部分视图不使用异步请求进行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是在我的一个异步调用中jquery文件( otf.js )在我的主页( index.cshtml )中更新我的餐馆列表,并在筛选餐馆结果的表单中输入搜索字词。 otf.js 在一个包中,并在布局视图中呈现。

is to make an asynchronous call in my jquery file(otf.js) that updates my restaurant list in my home page (index.cshtml) with a Search term entered in the form that filters the restaurant results. otf.js is in a bundle, and is rendered in the layout view.

是我输入一个搜索词并提交我的表单,并且列表按预期更新,但是当输入新搜索字词并点击提交,列表不会更改。每当我输入一个新的搜索结果并且点击提交时,它都需要改变。

is that I enter a search term and submit my form, and the list updates as expected, but when entering a new search term and clicking submit, the list does not change. It needs to change every time I enter a new search result and click submit.

$(function () { var ajaxFormSubmit = function () { var $form = $(this); var options = { url: $form.attr("action"), type: $form.attr("method"), data: $form.serialize() }; $.ajax(options).done(function (data) { var $target = $($form.attr("data-otf-target")); $target.replaceWith(data); }); return false; }; $("form[data-otf-ajax='true']").submit(ajaxFormSubmit); });

index.cshtml

index.cshtml

@model IEnumerable<OdeToFood.Models.RestaurantListViewModel> @{ ViewBag.Title = "Home Page"; } <hr /> <form method="get" action="@Url.Action("index")" data-otf-ajax="true" data-otf-target="#restaurantList"> <input type="search" name="searchTerm" /> <input type="submit" value="Search By Name" /> </form> <div id="restaurantList"> @Html.Partial("_Restaurants", Model) </div>

_Restaurants.cshtml(局部视图) h2>

_Restaurants.cshtml (a partial view)

@model IEnumerable<OdeToFood.Models.RestaurantListViewModel> @foreach (var item in Model) { <div> <h4>@item.Name</h4> <div> @item.City, @item.Country </div> <div> Reviews: @item.CountOfReviews </div> <hr /> </div> }

推荐答案

问题在于< div id =restaurantList> 包装在 @ Html.Partial(_ Restaurants,Model) line在 index.cshtml 中,它需要包装在局部视图 _Restaurants.cshtml

The problem is that <div id="restaurantList"> is wrapping around the @Html.Partial("_Restaurants", Model) line in index.cshtml, and it needs to be wrapped around the @foreach code block in the partial view _Restaurants.cshtml

我对这段代码做了这个修改,而且每次点击搜索按钮时餐厅列表都会更新,而不仅仅是第一次。

I made this change to my code, and presto, the restaurant list was updated every time i clicked the search button, not just the first time.

现在唯一的问题是为什么这会导致列表在第一次之后不更新?

Now the only question is why does this cause the list not to update after the first time?

更多推荐

部分视图不使用异步请求进行更新

本文发布于:2023-10-10 01:24:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1477306.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!