获取@html.隐藏值

Getting @html.hidden value

本文关键字:隐藏 @html 获取      更新时间:2023-09-26

我正在尝试从我的变量中获取值。

@Html.Hidden("myVar", 0);

此变量将根据我单击的链接进行更新

<script type="text/javascript">
    $(document).ready(function () {
        $(".resend").click(function (){
            $('#myVar').val(this.id);
            console.log(document.getElementById('myVar').value);
            $("#myModal").modal('show');
        });
    });
</script>

然后,此 JavaScript 将调用弹出模型。

<div id="myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <!— Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Confirmation</h4>
            </div>
            <div class="modal-body">
                <p>Are you sure you want to resend the registration link to the patient?</p>
            </div>
            <div class="modal-footer">
                <a href='@Url.Action("TherapistResendLinkToPatient", "Account", new { pid = I WANT TO CHANGE THIS TO THE VARIABLE #myVar })' style="text-decoration:none;">
                    <input type="button" class="btn btn-primary" value="Confirm" />
                </a>
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
            </div>
        </div>
    </div>
</div>

我想在我的弹出框中更改 pid = #myVar,但我不知道如何插入值。任何人都知道我如何将我的 pid 引用为 #myVar?

谢谢

为此,插入值的最佳方法是每次更改 #myVar 时更新 href 属性,使用 .attr() 包含新的值 #myVar。执行此操作的一个例子是在javascript中添加:

$('#confirmButton').attr('href','@Url.Action("TherapistResendLinkToPatient", "Account", new { pid = '+document.getElementById('myVar').value+' })');

您还需要在按钮周围的 href 包装器中添加一个 id,如下所示:

<a href='@Url.Action("TherapistResendLinkToPatient", "Account", new { pid = '+document.getElementById('myVar').value+' })' style="text-decoration:none;" id="confirmButton">
    <input type="button" class="btn btn-primary" value="Confirm" />
</a>

我希望这有所帮助

如果我理解您的要求,下面应该能够提供帮助。

目标是在数据目标标记中设置数据,然后在单击按钮时读取该数据。您的 ASP.NET 视图页面需要一个 forloop 我假设它已经存在,因为它从数据库创建用户列表。下面我假设您可以填写一些数据。

MVC View Page.CSHTML

@model PROJECT.Models.PatientRESEND
<table class="table table-striped">
  <thead>
   <th>Patient Name</th>
   <th>Patient Email</th>
   <th>Actions</th>
  </thead>
  <tbody>
@foreach (var item in Model)
{
   <tr>
    <td>@item.FirstName</td>
    <td>@item.LastName</td>
    <td><button data-target="@item.PatientID|@item.FirstName @item.LastName" id="resend" class="resend">Send Link</button></td>
   </tr>
}
 </tbody>
</table>

然后使用 jQuery 和 Bootstrap 向医生显示信息。

    $(document).ready(function () {
    $(".resend").click(function (event){
        event.preventDefault();
          var patientid =  $(this).data("target").split('|')[0];
        var patientName =  $(this).data("target").split('|')[1];
        $("#myModal").find('.name').text(patientName);
        var resethref = $("#myModal").find('.resendLink').attr('href');
        resethref = resethref+patientid;
        $("#myModal").find('.pid_output').text("Link: "+resethref);
                    $("#myModal").find('.resendLink').attr("href", resethref);
        $("#myModal").modal('show');
    });
});

 <td><button data-target="@item.PatientID|@item.FirstName @item.LastName" id="resend" class="resend">Send Link</button></td>

上面的代码将患者 ID 和患者名称由 | 分隔,然后我们使用 jQuery 来解析和更新引导模式的 DOM 对象。

请参阅JSFiddle的完整工作示例https://jsfiddle.net/fcrvz0gk/

我的团队设法解决了这个问题。

<script type="text/javascript">
    function DisableButton(b) {
        document.getElementById("cancel").disabled = true;
        b.disabled = true;
        b.form.submit();
    }
    $(document).ready(function () {
        $(".resend").click(function () {
            var id = this.id;
            $("#sendLink").attr('href', '/Account/TherapistResendLinkToPatient?pid=' + id);
            $("#myModal").modal('show');
        });
    });
</script>

我们不是存储变量,而是通过Javascript调用该方法。

感谢您的所有帮助,真的很感激!