gpt4 book ai didi

asp.net-mvc - 如何将数据从 View 模型传递给 javascript 函数?

转载 作者:行者123 更新时间:2023-12-01 09:29:30 25 4
gpt4 key购买 nike

例如,我有以下模型,我将其列表传递给 View :

public class ExampleViewModel
{
public int id { get; set; }
public string name { get; set; }

}

在我看来,我有以下几点:
@model List<ExampleViewModel>

<table>
<tr>
<th>Name</th>
<th>Action</th>
</tr>
@foreach (var x in Model)
{
<tr>
<td>
@Html.DisplayFor(m => x.name)
</td>
<td>
<input type="button" onclick="edit();" value="Edit">
</td>
</tr>
}
</table>

<script type="text/javascript">
function edit(x) {
window.location = "/Home/Edit?id=" + x
}
</script>

我遇到的问题是将 x.id 传递给 edit() 函数。我期望:
<input type="button" onclick="edit(@x.id);" value="Edit">

工作,但没有。

谢谢

最佳答案

尝试这个

<input type="button" onclick="edit(@x.id);" value="Edit">

关于asp.net-mvc - 如何将数据从 View 模型传递给 javascript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17568666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com