[英]Render a PartialView in a div without postback
我知道这个问题已被问过很多次,但无论我尝试什么,我都无法让它发挥作用。 我有一个 actionResult 接受三个参数并返回一个局部视图。 现在我想做的是获取三个元素的值并使用它们在 div 中重新渲染视图。 我尝试使用捕获的数据成功呈现 div,但我无法弄清楚 jquery 做错了什么
在脚本文件中包含了我尝试的最后一件事(尽管在每次尝试中,在放弃之前都有一些调整)
这是主视图中的 RenderAction(有效)
<div id="tables">
@{ Html.RenderAction("_Tables", new { date = "5/10/2019", time = "13:00", seats = "1" });}
</div>
返回所述 Partial 的操作结果
public ActionResult _Tables(string date, string time, int seats)
{
return PartialView("_Tables", checkTables(date,time,seats));
}
最后是脚本(searchTable 是字段附近的一个按钮。它们的值被成功捕获但加载不起作用)
$('#searchTable').click(function () {
var date = document.getElementById("datepicker").value;
var time = document.getElementById("time").value;
var seats = document.getElementById("seats").value;
alert(date);
//var data = JSON.stringify({
// 'date': date,
// 'time': time,
// 'seats': seats
//});
//$.ajax({
// type: "POST",
// url: '/Home/_Table/',
// data: data,
// dataType: "html",
// success: function (result) { success(result); }
//});
//function success(result) {
// alert(result);
// $("#tables").html(result);
// $("#tables").load("#tables")
//};
//$.ajax({
// url: "/Home/_Table",
// type: "GET",
// contentType: "application/json; charset=utf-8",
// data: data,
// success: function (data) {
// $('#target').html(data); // loading partialView into div
// }
//});
//$('#tables').load('@{ Html.RenderAction("_Tables", new { date = "' + date + '", time = "' + time + '", seats = "' + seats + '" });}');
$('#tables').load('@Url.Action("_Tables","Home")', { 'date': date, 'time': time, 'seats': seats });
alert(time);
//alert('@{ Html.RenderAction("_Tables", new { date = "' + date + '", time = "' + time + '", seats = "' + seats + '" });}');
});
我知道问题在于我缺乏了解,但我没有时间研究 ajax。 我的实习是基于在最后期限内“弄清楚”
我在您的代码中看到一条注释行:
$("#tables").html(result);
这就是你所需要的。 您应该使用从控制器返回的所有局部视图 html 填充您的 div。
假设您有一个带有id
表的div
<div id="tables">
您可以使用以下方法根据您的参数附加部分视图内容
$.ajax({
url: "/Home/_Tables?date=" + val + "&time="+ val +"&seats="+seats,
type: "POST",
cache: false,
success: function (result) {
$("#tables").empty();
$("#tables").html(result);
},
error: function () {
$("#tables").empty();
}
});
这将是主要视图 ajax function.and 在 controller 中执行以下操作
public ActionResult _Tables(string date, string time,string seats)
{
// here you can provide model,any viewbag values etc to the partial view and you will get corresponding html result
return PartialView("Yourpartial ViewName");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.