我为我的错字感到抱歉。一个视图将启动一个模式对话框,我需要它们之间的通信。我们正在使用JQuery。
我有一个名为Charges.cshtml的数据网格视图。数据网格的第一列可能具有span元素或link元素,具体取决于
属性,该属性将指示费用具有单个或多个描述。该视图如下所示。
如果费用包含多个描述,则用户将单击相应的描述链接(在这种情况下为Description2),然后将打开一个模式对话框,显示各种描述,如下所示
现在,在此模式对话框中,用户将确认/选择一个描述。现在,我需要关闭模式对话框并更新所选内容的描述
如下充电
这里最困难的部分是如何在两个视图之间传递数据。我可以通过控制器或JavaScript传递数据。
我尝试了多种方法将选定的费用从Charges.cshtml传递到LoanCharge控制器中的LoadLoanChargeDescriptions方法,例如json序列化,ViewData,ViewBag,TempData等,但没有用。我可以传递简单的数据类型,例如int,string,float,但不能传递整个对象。我觉得我需要将CurrentDescription和Descriptions传递给我的控制器,并且需要从它们传递到其他模块。我试图传递字符串列表,但由于在控制器中计数为0,所以无法看到如何在控制器中访问它们。我可以打开多个描述UI的弹出窗口(目前仅添加了Hello文本)
请参阅下面的代码段
Charges.cshtml
@model ChargeViewModel
@using (Html.FAFBeginForm())
{
<div>
<table>
<tbody>
<tr >
//.....
<td>
@if(Model.IsMultipleMatch)
{
var loanCharge = Model as ChargeViewModel;
if (loanCharge.IsMultipleMatch == true)
{
//string vm = @Newtonsoft.Json.JsonConvert.SerializeObject(loanCharge);
<span>
<a
onclick="ShowMatchingDescriptions('@Url.Action("LoadLoanChargeDescriptions", "LoanCharge")','', '920','500')">
@loanCharge.Description
</a>
</span>
}
}
else
{
<span>Model.Description</span>
}
</td>
</tr>
</tbody>
</table>
</div>
}
public class ChargeViewModel
{
public string Description {get;set;}
public bool IsMultipleMatch {get;set;}
public List<string> Descriptions {get;set;}
}
public class LoanChargeController
{
public ActionResult LoadLoanChargeDescriptions()
{
// get data here and pass/work on
return View("_PartialMultipleMatchPopup", null);
}
}
在Review.js中
function ShowMatchingDescriptions(popUpURL, windowProperties, w, h) {
try {
var left = (screen.width / 2) - (w / 2);
var top = (screen.height / 2) - (h / 2);
var properties = windowProperties + "dialogwidth:" + w + "px;dialogheight:" + h + "px;dialogtop:" + top + "px;dialogleft:" + left + "px;scroll:yes;resizable:no;center:yes;title:Matching Lender’s Fee;";
$.when(
window.showModalDialog(popUpURL, window, properties)
)
.then(function (result) {
var childWindow = result;
});
}
catch (err) {
alert("Error : " + err)
}
}
更新1
我更新了问题,并发布了更多详细信息。
提前致谢。
更新2
请在下面的链接中查看我的解决方案。
MVC pass model between Parent and Child Window
最佳答案
为什么不使用AJAX传递数据?
function ChargeViewModel() {
this.Description ='';
this.IsMultipleMatch =false;
}
var chargeViewModel= new ChargeViewModel();
var data = JSON.stringify({ 'chargeViewModel': chargeViewModel });
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'html',
type: 'POST',
url: '@Url.Action("LoadLoanChargeDescriptions", "LoanChargeController")',
data: data,
success: function (result) {
//result will be your partial page html output
},
failure: function (response) {
}
});
然后,您必须像这样更改控制器:
public ActionResult LoadLoanChargeDescriptions(ChargeViewModel chargeViewModel)
{
// get data here and pass/work on
return View("_PartialMultipleMatchPopup", null);
}
让我知道您有疑问。