我有下面的jQuery脚本来显示模型对话框,以便从表中编辑所选的项:
<script type="text/javascript">
$(function () {
$('#dialog').dialog({
autoOpen: false,
width: 800,
resizable: false,
title: 'Edit Person',
modal: true,
open: function (event, ui) {
var url = '@Html.Raw(Url.Action("Edit", "Person", new {
id = **Need item.Id here**,
selectedPersonFor = Model.SelectedPersonFor,
selectedPersonForId = Model.SelectedPersonForId,
clientAccountId = Model.ClientAccountId
}))';
$(this).load(url);
},
buttons: {
"Save": function () {
$(this).dialog("save");
// prevent the default action, e.g., following a link
return false;
},
"Close": function () {
$(this).dialog("close");
// prevent the default action, e.g., following a link
return false;
}
}
});
$('#editperson').click(function () {
$('#dialog').dialog('open');
});
});
当用户单击下表中特定行的编辑按钮(仅显示表的部分标记)时,将打开此对话框:
@foreach (var item in Model.Persons)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title.Name)
</td>
......
<td>
<button id="editperson">Edit</button>
</td>
</tr>
}
我需要做的是将item.id值从foreach循环中获取到我的jQuery脚本中,以填充url变量中的id值(我已经用文本 item.Id here标记了脚本中的位置)。
是否有办法实现这一目标--还是我需要以一种不同的方式来处理这个问题?
发布于 2012-01-09 01:47:32
使用上面的结构实现这一目的的一种方法是添加一个函数来手动触发对话框,并在显示它之前设置'working‘person ID,如下所示:
var workingPersonId;
function showPersonDialog(personId) {
workingPersonId = personId;
$('#dialog').dialog('open');
}
$('#dialog').dialog({
autoOpen: false,
width: 800,
resizable: false,
title: 'Edit Person',
modal: true,
open: function (event, ui) {
var url = '@Html.Raw(Url.Action("Edit", "Person", new {
id = workingPersonId,
selectedPersonFor = Model.SelectedPersonFor,
selectedPersonForId = Model.SelectedPersonForId,
clientAccountId = Model.ClientAccountId
}))';
$(this).load(url);
},
buttons: {
"Save": function () {
$(this).dialog("save");
// prevent the default action, e.g., following a link
return false;
},
"Close": function () {
$(this).dialog("close");
// prevent the default action, e.g., following a link
return false;
}
}
});
用类似于:
<button id="editperson" onclick="showPersonDialog(@item.ID);return false;">Edit</button>
发布于 2012-01-09 01:46:32
首先,在javascript文件中设置一个变量,函数和对话框都可以访问该变量。您只需在单击中设置该变量,然后在对话框中使用它。但问题是拿到身份证了。以下是一些解决办法:
解决方案1-隐藏字段
您可以从按钮或其他地方在同一个TD中添加一个隐藏字段。
<td>
@Html.HiddenFor(i => item.Id)
<button id="editperson">Edit</button>
</td>
单击该按钮时,只需执行以下操作:
$(this).parent().find('input').first()
这会得到隐藏的区域。然后你可以从它得到价值,并做任何你想做的事情。
解决方案2-按钮中的数据属性
您可以在包含id的按钮中添加“data”参数。
<button id="editperson" data-myitemid="@(Item.Id)">Edit</button>
然后,可以在jquery函数中获得如下所示的id:
var oItemId = $(this).attr('data-myitemid');
解决方案3-触发带有参数的函数。
您可以使用onclick触发javascript函数,只需传递参数。
<button id="editperson" onclick="SomeJavascriptFunctionWithAParameter(@(Item.Id));">Edit</button>
https://stackoverflow.com/questions/8786209
复制相似问题