我有一个使用剃刀由foreach循环生成的表,我想 当我单击模态旁边的“编辑”按钮时,能够自动填充模态,但我似乎可以访问剃刀在屏幕上显示的值。
发现的一些答案是
1)
<td>
@Html.DisplayFor(modelItem => item.Id, new {class= "something"})
</td>
2)
<td>
<span class="something">
@Html.DisplayFor(modelItem => item.Id)
</span>
</td>
仅使用
的javascriptvar test = document.getElementsByClassName(".something");
,然后尝试在单击按钮时将其显示在警报中,但我要么得到 如果我执行alert(test [0])则未定义,或者如果我执行它则为集合 警报(测试)
html
@model IEnumerable<EpisodeSoftTest.Models.Customer>
@{
ViewBag.Title = "Client Side";
}
<h2>@ViewBag.Title</h2>
<p>
<h3>Manage Customers</h3>
<a class="openModal"><span style="float:right" class="glyphicon glyphicon-plus-sign"></span></a>
</p><br /><br />
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Id)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
<a class="openModal"><span class="glyphicon glyphicon-pencil"></span></a>
</td>
</tr>
}
</table>
<div id="my-modal" class="modal">
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2>Customer</h2>
</div>
<div class="modal-body">
<form>
<table class="table">
<tr>
<th>Name</th>
<th>Email</th>
</tr>
<tr>
<td>
<input id="Name" type="text" placeholder=" John Smith" />
</td>
<td>
<input id="Email" type="email" placeholder="JSmith@example.com" />
</td>
</tr>
</table>
</form>
</div>
<div class="modal-footer">
<input style="float:right" class="save" type="submit" value="Save Changes" />
</div>
</div>
</div>
我想要做的是,当我单击“编辑”按钮时,根据ID自动填充模态字段;如果按下“添加”按钮,则使用空字段打开模态。