我的视图模型包含要迭代的对象列表,每个对象都有一个与之关联的特定类。我的目标是单击该项目以将其打开以查看,但是我不清楚如何在我的jquery click函数中获取该行的ID。

            foreach (var item in Model.PatientViewModel)
            {
                    <div class="patientBox patientBox-unselected">
                        <h7>
                            <div class="pvb-mrn">MRN: @Html.DisplayFor(modelItem => item.MRN)</div>
                            <div class="pvb-dob">DOB: @Html.DisplayFor(modelItem => item.DOB)</div>
                            <br />
                            <div class="pvb-link">
                                @Html.ActionLink("Update Patient >", "Edit", new { id = item.PatientID })
                            </div>
                        </h7>
                    </div>
            }


然后我的脚本带​​有测试警报,只是为了确保我点击了该函数,该函数可以正常工作,但是如何在此处获取被单击项的ID?

    $('.patientBox').click(function () {
        window.location.href("/View/" + @item.ID);
    })


查看模型:

public class PatientScreenViewModel
{
    public List<PatientDTO> PatientViewModel { get; set; }

    public PatientSearchDTO SearchViewModel { get; set; }
}

最佳答案

item.ID放在html属性中,并使用jQuery进行获取,如下所示:

foreach (var item in Model.PatientViewModel){
   <div class="patientBox patientBox-unselected" data-item-id="<%= item.ID %>">
                   ...
                   ...
   </div>
}


jQuery的:

$('.patientBox').click(function () {
    window.location.href("/View/" + this.getAttribute('data-item-id');
})

关于javascript - 在jQuery中获取模型绑定(bind)对象属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41470184/

10-12 00:46
查看更多