[英]Change a variable when a link is clicked
I have a page that loads the last 25 rows from a database and displays them in a table. 我有一个页面可以从数据库中加载最后25行,并在表格中显示它们。 I want to be able to click a link and have a modal popup that contains more information.
我希望能够单击一个链接,并有一个包含更多信息的模式弹出窗口。 I've got everything working except for knowing which row Id was clicked.
除了知道单击了哪一行ID之外,我一切正常。 Below is currently what I have.
下面是我目前所拥有的。
Model.ClickedId
never changes from the default value so the popup has the same message everytime. Model.ClickedId
永远不会更改默认值,因此每次弹出窗口都会显示相同的消息。
How can I make it so ClickedId
on the backend is set to item.Id
when the link is clicked? 我怎样才能使它所以
ClickedId
在后端设置为item.Id
链接被点击的时候?
Backend: 后端:
public int ClickedId { get; set; } = 0;
Front end: 前端:
@foreach (var item in Model.SFException)
{
<tr>
<td>
<a href="#" data-toggle="modal" data-target="#exampleModalCenter" onclick="@Model.ClickedId=@item.Id">View</a> <!-- Set ID to item.ID? -->
</td>
<td>
@Html.DisplayFor(modelItem => item.ObjectType)
</td>
<td>
@Html.DisplayFor(modelItem => item.ObjectKeyProperty)
</td>
<td>
@Html.DisplayFor(modelItem => item.ObjectKeyValue)
</td>
...
And the modal code where I am trying to display more information: 以及我试图显示更多信息的模式代码:
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterTitle">Exception Details</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
@Html.DisplayFor(model => model.SFException[Model.ClickedId].StackTraceErrorMessage)
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
There are a couple of ways I've done this in the past. 我过去有几种方法可以做到这一点。 Either:
或者:
Put the 'StackTraceErrorMessage' on the page somewhere like 将“ StackTraceErrorMessage”放在页面上,例如
<td class="open-modal" data-itemId="@item.Id">
View
<input type="hidden" value="@item.StackTraceErrorMessage" />
</td>
Then in JS look for when the 'View' text is clicked, move the StackTraceErrorMessage from the hidden area to the modal html and display the modal html 然后在JS中查找单击“查看”文本的时间,将StackTraceErrorMessage从隐藏区域移至模式html并显示模式html
$(document).ready(function() {
$(".open-modal").click(function() {
// get the item id from the clicked on element
var itemId = $(this).data("itemId");
// get the relevant StackTraceErrorMessage and put in the modal html
var message = $(this).find('input').val();
$('.modal-body').html(message);
// show the modal html (presumably this has styles associated to make it look like a dialog)
$('.modal).show();
});
)};
The link in your row would look something like this: 您行中的链接如下所示:
<td data-itemId="@item.Id" class="show-row-details">View</td>
Where the item id is stored as an attribute in the element and a class is attached so we can watch for clicks. 其中,商品ID作为属性存储在元素中,并附加了一个类,因此我们可以观察点击情况。 In your js you would then look for any clicks like :
在您的js中,然后您会寻找任何类似的点击:
$(document).ready(function() {
$(".show-row-details").click(function() {
// get the item id from the clicked on element
var itemId = $(this).data("itemId");
// make a request to the backend for more info
$.ajax({
url: baseUrl + "YourController/YourAction",
data: { itemId : itemId },
success: function (data) {
// put the data returned into the popup element on our page and make it visible
$('#popup').html(data);
$('#popup').show();
}
})
});
)};
So to support this on your page you would need an elment ready to recieve data from the backend 因此,要在您的页面上支持此功能,您需要准备就绪才能从后端接收数据
<div id="popup" style="display:none"></div>
and also you would need a controller and action on your backend that is going to return the Html that you want to display in the popup div (pretty much just an action that loads a partial view (ie no layout) with your 'modal code' in it). 并且您还需要在后端上使用一个控制器和一个操作来返回要在弹出div中显示的HTML(几乎只是一个用“模态代码”加载部分视图(即无布局)的操作)在里面)。
Note: I haven't actually tried the above code, so there may be some syntax errors etc 注意:我实际上没有尝试过上面的代码,因此可能存在一些语法错误等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.