[英]render partial view from jquery not working
I have some issues with rendering a partial view using jquery. 我有一些使用jquery渲染局部视图的问题。 Even if it was asked more than once ( here ), I didn't found a solution that worked for me: My view:
即使它不止一次被问到( 这里 ),我也没有找到适合我的解决方案:我的观点:
@model IEnumerable<OdeToFood.Models.RestaurantListViewModel>
@{
ViewBag.Title = "Home Page";
}
<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>
@Html.Partial("_Restaurants", Model)
My partial view: 我的部分观点:
@model IEnumerable<OdeToFood.Models.RestaurantListViewModel>
<div id="restaurantList">
@foreach (var item in Model)
{
<div>
<h4>@item.Name</h4>
<div>@item.City, @item.Country</div>
<div>REviews: @item.CountOfReviews</div>
<hr />
</div>
}
</div>
otf.js: otf.js:
$(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 action of my controller: 我的控制器的索引动作:
public ActionResult Index(string searchTerm = null)
{
var model =
_db.Restaurants
.OrderByDescending(r => r.RestaurantReviews.Average(a => a.Rating))
.Where(r => searchTerm == null || r.Name.StartsWith(searchTerm))
.Select(r => new RestaurantListViewModel
{
Id = r.Id,
Name = r.Name,
City = r.City,
Country = r.Country,
CountOfReviews = r.RestaurantReviews.Count()
}
)
.Take(10);
if (Request.IsAjaxRequest())
{
return PartialView("_Restaurants", model);
}
return View(model);
}
My issue is that nothing happens when I press search button. 我的问题是当我按下搜索按钮时没有任何反应。 I've added an test alert like:
我添加了一个测试提醒,例如:
$.ajax(options).done(function (data) {
var $target = $($form.attr("data-otf-target"));
alert('test');
$target.replaceWith(data);
});
and the alert showed when I've clicked the search button, but in site nothing happens. 当我点击搜索按钮时,警报显示,但在网站上没有任何反应。 Anyone knows what is my issue?
谁知道我的问题是什么?
You have #restaurantlist
as the target, whereas div id is actually restaurantList
. 你有
#restaurantlist
作为目标,而div id实际上是restaurantList
。 Capital "L". 资本“L”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.