簡體   English   中英

@ html.Action通過輸入類型傳遞值

[英]@html.Action pass value from an input type

我想問一下如何將值從輸入字段傳遞到@ Html.Action的第三個參數,以便可以訪問控制器方法。 這是我的代碼:

在我看來:這是將id傳遞到輸入的腳本。

<script type="text/javascript">
    $(document).on("click", ".desktop_id", function () {
        var deskId = $(this).data('id');
        $(".modal-body #idd").val(deskId);
    });
</script>

這是顯示動作局部視圖的模態

<a data-toggle="modal" data-id="@item.desktop_info_id" title="Add this item" class="desktop_id" href="#myModal1"><i title="Actions" class="fa fa-th"></i></a>
<!-- Modal -->
<div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel1">Actions</h4>
            </div>
            <div class="modal-body">
                <input type="text" name="idd" id="idd" value=""/>
                @Html.Action("Transaction", "Desktop", new {id=---need to be field---})
            </div>
            <div class="modal-footer">
                <button type="button"class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

@ Html.Action(“ Transaction”,“ Desktop”,新的{id = ---需要填寫---})第三參數如何可能從輸入字段中訪問id值,因此我可以將其傳遞給控制器​​? 謝謝。

嘗試直接從jQuery函數重定向到您的操作:

<script type="text/javascript">
    $(document).on("click", ".desktop_id", function () {
        var deskId = $(this).data('id');
        var url = '@Html.Raw(Url.Action("Transaction", "Desktop", new { id = "_id" }))';
        url = url.replace("_id", deskId);
        window.location(url);
    });
</script>

我想我了解您的需求。 您的模態取決於id,因此您可以像直接使用它

<a data-toggle="modal" data-id="@item.desktop_info_id" title="Add this item" class="desktop_id" href="#@item.desktop_info_id"><i title="Actions" class="fa fa-th"></i></a>

注意 :在href="#@item.desktop_info_id"使用動態ID

還要為模式設置相同的ID: id="@item.desktop_info_id"

<div class="modal fade" id="@item.desktop_info_id" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel1">Actions</h4>
            </div>
            <div class="modal-body">
                <input type="text" name="idd" id="idd" value=""/>
                @Html.Action("Transaction", "Desktop", new {id=item.desktop_info_id})
            </div>
            <div class="modal-footer">
                <button type="button"class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

更新
在這里擺弄樣本,似乎一切正常: dotnetfiddle

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM