簡體   English   中英

如何使用 Ajax 將參數傳遞給模態表單

[英]How to pass a parameter to a modal form using Ajax

我有一個顯示所選報告的費用列表的剃須刀頁面。 我在頁面上有一個“添加費用”按鈕,可以顯示一個模式。 模態是表單的局部視圖。 我需要做的是將 ExpenseId 傳遞給模態。 我可以像這樣從 url 獲取 Id

 @{ var expenseId = Request.Url.Segments[3]; }

該按鈕目前看起來像這樣

<button type="button" data-toggle="modal" data-target="#expenseModal_@expenseId" data-id="@expenseId" class="btn btn-primary" id="addExpenses">
                        Add Expense
                    </button>

這里面有一些東西我不知道我是否需要它們。 我在嘗試不同的東西。

模態

<!-- MODAL -->
<div class="modal fade" id="expenseModal_@expenseId" tabindex="-1" role="dialog" aria-labelledby="expenseModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <h4 class="modal-title" id="expenseModalLabel"> Expences </h4>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div> <!-- MODEL HEADER-->
        <div class="modal-body">
        </div> <!-- MODAL BODY-->
    </div>
</div>

Javascript

    <script type="text/javascript">
    $(document).ready(function () {
        $("#addExpenses").click(function () {

            $(".modal-body").html('');

            $.ajax({

                type: 'GET',
                url: '@Url.Action("_ExpenseForm", "Admin")',
                data: { type: $(this).attr("data-type") },
                success: function (response) {
                    $(".modal-body").html(response);
                    $("#expenseModal").modal('show');
                },
                error: function () {
                    alert("Something went wrong");
                }

            });

        });
    });
</script>

必須在表單中插入費用 ID,以便在保存時將其保存到正確的費用報告中。

控制器動作

    ExpensesDataAcessLayer objexpense = new ExpensesDataAcessLayer();
    public ActionResult ExpenseReports()
    {
        return View(db.ExpenseReports.ToList());
    }

    public ActionResult Expenses(int ExpenseId)
    {
        return View(db.Expenses.Where(x => x.ExpenseId == ExpenseId).ToList());
    }

    public ActionResult _ExpenseForm()
    {
        CustomerEntities customerEntities = new CustomerEntities();
        List<SelectListItem> categoryItem = new List<SelectListItem>();
        ExpensesViewModel casModel = new ExpensesViewModel();
        List<ExpenseTypes> expensetypes = customerEntities.ExpenseType.ToList();
        expensetypes.ForEach(x =>
        {
            categoryItem.Add(new SelectListItem { Text = x.CategoryItem, Value = x.ItemCategoryId.ToString() });
        });
        casModel.ExpenseTypes = categoryItem;

        return View(casModel);
    }

謝謝你的幫助!

您可以將expenseId存儲到隱藏字段中,如下所示

<input id="expenseId" name="expenseId" type="hidden" value="@Request.Url.Segments[3]">

然后你可以像這樣

 $("#addExpenses").click(function () {
var expenseId = $("#expenseId").val();
// after code here

更新

您可以像這樣獲得expenseId

var expenseId = $(this).attr("data-id")

然后你可以將它分配給隱藏字段或Model任何地方,像這樣

<!-adding aditional input into HTML in MODEL-!>
<input id="expenseId" name="expenseId" type="hidden" value="">

<!- Javascript-!>
var expenseId = $(this).attr("data-id")
expenseId.val(expenseId );

暫無
暫無

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

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