简体   繁体   English

如何从Modal表单结果中插入ajax?

[英]How to ajax insert into value from Modal form result?

I am building an Invoice form where the user must select a customer from thousands of records. 我正在构建一个Invoice表单,用户必须从数千条记录中选择一个客户。 In order to allow users to search & select, I want to use a modal form to select the desired customer and then insert the customer id & name into the original form. 为了允许用户搜索和选择,我想使用模态表单来选择所需的客户,然后将客户ID和名称插入到原始表单中。 (I believe this method is a work around for the "no-nested-forms" rule.) (我相信这种方法可以解决“无嵌套形式”规则。)

Invoice Form: 发票表格:

Customer : [Select Customer Button] - [Display Name after selected] [hidden: customer_id] 客户:[选择客户按钮] - [选择后显示名称] [隐藏:customer_id]

Amount: [normal form stuff] 金额:[普通表格]

Modal Form: 模态形式:

Search [ ] 搜索[]

[Table of Customers, filter by search] [select this customer button] [客户表,按搜索过滤] [选择此客户按钮]

I don't think I need to have a [http post] method to capture the Modal Form's selection - instead I just want to use javascript to update the Invoice Form's value for Customer ID (hidden) & Customer Name. 我认为我不需要[http post]方法来捕获模态表单的选择 - 而是我只想使用javascript来更新发票表单的客户ID(隐藏)和客户名称的值。

So far I have: 到目前为止,我有:

Controller: 控制器:

public ActionResult Modal()
{
     return PartialView();
}
public ActionResult SaveInvoice()
{
     return View();
}
[HttpPost]
public ActionResult SaveInvoice(Invoice invoice)
{
     dataManager.Save(invoice);
     return RedirectToAction("Index");
}

Modal Partial: 模态部分:

--have not implement search yet - for now trying to get insertion to work from number text box - 尚未实现搜索 - 现在尝试从数字文本框中插入工作

@model Models.Customer
<div class="modal-content">
    <div class="modal-header"> Select Customer </div>
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary()
        @Html.AntiForgeryToken()
        <div class="modal-body">
            <div class="form-horizontal">
                <div class="form-group">
                    @Html.LabelFor(model => model.id, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.id, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.id, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Save" class="btn btn-default" id="save-customer-id" />
                </div>
            </div>
        </div>
    }
</div>

Invoice View 发票视图

@model Models.Invoice
<div class="form-horizontal">
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.customerId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <button id="CustomerBtn" class="btn btn-primary btn-lg" onclick="ShowModal()">Select Customer</button>
            @Html.EditorFor(model => model.customerId, new { htmlAttributes = new { @class = "form-control", id= "customer-id" } })
            @Html.ValidationMessageFor(model => model.customerId, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>


<div id="CustomerModal" class="modal">
    <div id="CustomerContainer" class="modal-dialog">
        @Html.Partial("Modal")
    </div>
</div>

Javascript 使用Javascript

<script type="text/javascript">
    function ShowModal() {
        $('#CustomerModal').modal('show');
    };
</script>

Currently the code shows the form and the button opens the modal with form correctly. 目前代码显示表单,按钮正确打开表单模式。

Thanks in advance! 提前致谢! I've been searching around but haven't seen anything about inserting from the modal back to the "background page". 我一直在四处搜索,但没有看到任何关于从模态插入“背景页面”的内容。 (And I haven't used AJAX much). (而且我还没有使用过很多AJAX)。

Using: 使用:

  • Visual Studio 2015 Visual Studio 2015
  • ASP.NET MVC Entity Framework ASP.NET MVC实体框架
  • Bootstrap (tried using TwitterBootstrapMVC without any luck) Bootstrap(尝试使用TwitterBootstrapMVC没有任何运气)

You can insert value of the selected customer from modal to the background form using the bootstrap event 'hidden.bs.modal' or 'hide.bs.modal'. 您可以使用引导事件'hidden.bs.modal'或'hide.bs.modal'将所选客户的值从模态插入到背景表单中。

DEMO DEMO

 $('#myModal').on('hidden.bs.modal', function () { $("#customerName").val($("#searchCustomer").val()); }); 
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="container"> <form> <label> Customer</label>&nbsp;&nbsp;<input type="text" id="customerName"> <button type="button" class="btn btn-link btn-xs" data-toggle="modal" data-target="#myModal">Search Customer</button> <br/> <button type="submit" class="btn btn-default">Save</button> </form> <!-- Modal --> <div class="modal fade" id="myModal" 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">Search Customer</h4> </div> <div class="modal-body"> Customer <input type="text" id="searchCustomer" value="Anil Panwar or id here"> <button type="button" class="btn btn-default btn-xs" data-dismiss="modal">Select</button> </div> <div class="modal-footer"> </div> </div> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM