简体   繁体   English

从Html.ActionLink将模型项ID传递给jquery函数

[英]Pass model item id to jquery function from Html.ActionLink

I want to be able to pass the id of each item in below code to jQuery function using Html.ActionLink(). 我希望能够使用Html.ActionLink()将以下代码中每个项目的ID传递给jQuery函数。

@foreach (var item in Model)
    {
        if (item.AppointmentStatus == "P")
        {   
        <div class="appointment-main-block" role="menuitem" tabindex="20">
            <div class="appointment-heading">
                @Html.DisplayFor(modelItem => item.Speciality)
            </div>
            <div class="appointmenttitle">
                Date
               <br />
                <br />
                Clinician
               <br />
                <br />
                Location
            </div>
            <div class="appointmentdata">
                @Html.DisplayFor(modelItem => item.AppointmentDate)
                <br />
                <br />
                @Html.DisplayFor(modelItem => item.ClinicianName)
                <br />
                <br />
                @Html.DisplayFor(modelItem => item.Location)
            </div>

            <div class="appointmentbutton appointmentbuttonclickable">
                View @Html.ActionLink(" ", "Details", "MyAppointments",  new { id = item.AppointmentIdentifier.id }, new { onclick = "getAppointmentRef( item.AppointmentIdentifier.id)" })
                <br />
                <img src="/Content/images/icons/view.png" alt="view icon" />
            </div>
        </div>
        }
    }

// jQuery - Very Basic for now also considered
$(".appointmentbutton").click(getAppointmentRef);
    function getAppointmentRef(id) {
                alert(id);
            }

The function is called ok but even if I pass a 'hello' it seems to pass an JSON string none of whose params are the 'id' i,e. 该函数称为ok,但是即使我传递了“ hello”,它似乎也传递了JSON字符串,其参数都不是“ id”。 123456789 123456789

How do I make the Actionlink pick up the id for that model item? 如何使Actionlink拾取该模型项目的ID?

Firstly you have a syntax error there. 首先,您在这里存在语法错误。 This, I believe, should read: 我认为,应改为:

onclick = "getAppointmentRef( @item.AppointmentIdentifier.id)"

Or 要么

onclick = "getAppointmentRef( " + @item.AppointmentIdentifier.id + ")"

One of the two. 两者之一。

However, principally I would take a different approach: 但是,原则上我会采用不同的方法:

@Html.ActionLink("Link text", "Details", "MyAppointments",  new { id = item.AppointmentIdentifier.id }, new { @class = "myLink", data_id = item.AppointmentIdentifier.id })

Then in jQuery: 然后在jQuery中:

$(".myLink").click(function () {
    alert($(this).data("id"));
});

That way, you have a much clearer separation of concerns. 这样,您就可以更清晰地将关注点分离。

I hope this helps. 我希望这有帮助。

For your button you can use something like - 对于按钮,您可以使用类似-

<input type="button" data-apptid="@item.AppointmentIdentifier.id" class="myLink" value="Click me!" />

And your jQuery could be like Moby mentioned - 您的jQuery可能就像Moby提到的那样-

$(".myLink").click(function () {
    document.location.href = "/MyAppointments/Details/" & $(this).data("apptid");
});

This way if you needed to do something in jQuery besides just load the new page, you could do it there and have the function determine whether you redirect or not. 这样,如果除了加载新页面外,还需要在jQuery中执行某些操作,则可以在其中执行,并让函数确定是否重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM