获取@html.hidden值

时间:2016-02-06 15:12:26

标签: javascript html razor

我试图从变量中获取值。

@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?

由于

3 个答案:

答案 0 :(得分:0)

要实现此目的,插入值的最佳方法是每次更改#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>

我希望帮助

答案 1 :(得分:0)

如果我理解您的要求,以下应该可以提供帮助。

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

MVC查看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来解析和更新bootstrap模式的DOM对象。

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

答案 2 :(得分:0)

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

<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调用该方法。

感谢您的帮助,非常感谢!