在MVC更新局部视图之前打开JQuery对话框
我试图在按钮单击事件期间打开对话框模式窗口,当我打开此窗口时,我希望能够调用一个视图,该视图获取最新信息,并在加载时使用正确的信息更新模式对话框。但它总是抓取第一条记录并显示出来。查看代码时,它会打开对话框,然后运行操作结果的代码,但视图从未使用正确的记录进行更新 所以我有一个运输地址的下拉列表。我想得到我得到的所选记录的ID,将其传递给ActionResult方法,我已经完成了这项工作,获取我需要的数据,打开一个对话框,并将我想要显示的信息传递给它,然而,它并没有更新我传递给它的内容,因为模态已经弹出了原始模态值,我猜是列表中的第一个地址,而不是我当前选择的地址,如果这有意义的话 控制器:在MVC更新局部视图之前打开JQuery对话框,jquery,asp.net-mvc,asp.net-mvc-4,Jquery,Asp.net Mvc,Asp.net Mvc 4,我试图在按钮单击事件期间打开对话框模式窗口,当我打开此窗口时,我希望能够调用一个视图,该视图获取最新信息,并在加载时使用正确的信息更新模式对话框。但它总是抓取第一条记录并显示出来。查看代码时,它会打开对话框,然后运行操作结果的代码,但视图从未使用正确的记录进行更新 所以我有一个运输地址的下拉列表。我想得到我得到的所选记录的ID,将其传递给ActionResult方法,我已经完成了这项工作,获取我需要的数据,打开一个对话框,并将我想要显示的信息传递给它,然而,它并没有更新我传递给它的内容,因为模态
public ActionResult PublicInfo(string widgetZone)
{
var address = _workContext.CurrentCustomer.Addresses
.FirstOrDefault(a => a.Id == _workContext.CurrentCustomer.ShippingAddress.Id);
PublicInfoModel model = new PublicInfoModel();
model.FirstName = address.FirstName;
model.LastName = address.LastName;
model.Address1 = address.Address1;
model.Address2 = address.Address2;
model.City = address.City;
model.StateProvinceId = 1;
model.StateProvinceName = address.StateProvince.Name;
model.CountryId = 1;
model.CountryName = address.Country.TwoLetterIsoCode;
model.PostalCode = address.ZipPostalCode;
return View("Nop.Plugin.Widgets.AddressVerification.Views.WidgetsAddressVerification.PublicInfo", model);
}
[HttpPost]
public ActionResult PublicInfo(int id)
{
ViewBag.selectedShippingId = id;
var address = _workContext.CurrentCustomer.Addresses
.FirstOrDefault(a => a.Id == id);
PublicInfoModel model = new PublicInfoModel();
model.FirstName = address.FirstName;
model.LastName = address.LastName;
model.Address1 = address.Address1;
model.Address2 = address.Address2;
model.City = address.City;
model.StateProvinceId = 1;
model.StateProvinceName = address.StateProvince.Name;
model.CountryId = 1;
model.CountryName = address.Country.TwoLetterIsoCode;
model.PostalCode = address.ZipPostalCode;
return View("Nop.Plugin.Widgets.AddressVerification.Views.WidgetsAddressVerification.PublicInfo", model);
}
型号:
@model Nop.Plugin.Widgets.AddressVerification.Models.PublicInfoModel
<div id="dialog-modal" style="display:none;">
@Html.Partial("Nop.Plugin.Widgets.AddressVerification.Views.Shared._Address", Model)
</div>
<script type="text/javascript">
$(document).ready(function () {
$("#shipping-buttons-container .new-address-next-step-button")[0].onclick = null;
$("#shipping-buttons-container .new-address-next-step-button").click(function () {
newContent();
if (e.preventDefault) {
// For modern browsers
e.preventDefault();
}
else {
// For older IE browsers
e.returnValue = false;
}
//Shipping.save();
});
function newContent() {
$(function () {
var selectedShippingAddressId = $('#shipping-address-select').val();
//alert(selectedShippingAddressId);
if (selectedShippingAddressId != null) {
$('#dialog-modal').dialog({
autoOpen: true,
width: 500,
resizable: false,
title: 'An updated address has been determined, would you like to use this one?',
modal: true,
open: function (event, ui) {
var url = '@Url.Action("PublicInfo", "WidgetsAddressVerification")';
$.post(url, { id: selectedShippingAddressId });
},
buttons: {
"Close": function () {
$(this).dialog("close");
}
}
});
}
});
}
});
</script>
任何帮助都将不胜感激。只是为了让当前选择的信息在模态中弹出。我不完全确定您希望它如何流动,但要更新模态的内容,您只需要更新对话框模态div,并使用从ajax中下拉的部分视图,为js调用以下内容
$("#shipping-buttons-container .new-address-next-step-button").click(function() {
$.ajax({
type: 'post',
url: "@Url.Action("PublicInfo", "WidgetsAddressVerification")",
success: function(data) {
$("#dialog-modal").html(data);
$('#dialog-modal').modal('show');
}
});
});
控制器在部分视图中传回html,因此它不包括_布局模板,只是原始html
public PartialViewResult PublicInfo(string widgetZone)
{
var address = _workContext.CurrentCustomer.Addresses
.FirstOrDefault(a => a.Id == _workContext.CurrentCustomer.ShippingAddress.Id);
PublicInfoModel model = new PublicInfoModel();
model.FirstName = address.FirstName;
model.LastName = address.LastName;
model.Address1 = address.Address1;
model.Address2 = address.Address2;
model.City = address.City;
model.StateProvinceId = 1;
model.StateProvinceName = address.StateProvince.Name;
model.CountryId = 1;
model.CountryName = address.Country.TwoLetterIsoCode;
model.PostalCode = address.ZipPostalCode;
return PartialView("Nop.Plugin.Widgets.AddressVerification.Views.WidgetsAddressVerification.PublicInfo", model);
}
Jquery默认在异步模式下工作 在使用post时
$.post(url, { id: selectedShippingAddressId });
你可以用
$.ajax({
url: url,
data: { id: selectedShippingAddressId },
success: success,
type: 'POST',
async:false
});
现在,当上述代码经过处理时,pop将始终加载。使用ajax调用获取页面上的部分内容,然后打开对话框。看看我的答案,我已经试过了。它正确地进入了partialviewresult,但现在它没有打开对话框窗口并显示信息。您对模式代码使用了什么?我使用的是bootstrap,因此使用了.modal'show'命令。实际上,我现在就可以使用它了。谢谢你的帮助。