简体   繁体   English

如何使用 Ajax 将参数传递给模态表单

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

I have a razor page that displays a list of expenses for the Report selected.我有一个显示所选报告的费用列表的剃须刀页面。 I have an "Add Expense" button on the page that brings up a modal.我在页面上有一个“添加费用”按钮,可以显示一个模式。 The modal is a partial View of the form.模态是表单的局部视图。 What i need to do is pass the ExpenseId to the modal.我需要做的是将 ExpenseId 传递给模态。 I can get the Id from the url like this我可以像这样从 url 获取 Id

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

the button currently looks like this该按钮目前看起来像这样

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

There are a few things in this that i do not know if i even need them.这里面有一些东西我不知道我是否需要它们。 I was trying different things.我在尝试不同的东西。

Modal模态

<!-- 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 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>

The expense Id has to be inserted in the form so that when it is saved it saves it to the correct Expense report.必须在表单中插入费用 ID,以便在保存时将其保存到正确的费用报告中。

Controller actions控制器动作

    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);
    }

Thanks for your help!谢谢你的帮助!

You can store expenseId into hidden field, like this您可以将expenseId存储到隐藏字段中,如下所示

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

Then you can get like this然后你可以像这样

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

Updated更新

You can get expenseId like this您可以像这样获得expenseId

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

Then you can assign it to hidden field or anywhere in Model , Like this然后你可以将它分配给隐藏字段或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