【问题标题】:How to ajax insert into value from Modal form result?如何将 ajax 插入模态表单结果中的值?
【发布时间】:2017-02-21 00:28:49
【问题描述】:

我正在构建一个发票表单,用户必须在其中从数千条记录中选择一个客户。为了允许用户搜索和选择,我想使用模态表单来选择所需的客户,然后将客户 ID 和名称插入到原始表单中。 (我相信这种方法可以解决“无嵌套形式”规则。)

发票表格:

客户:[选择客户按钮] - [选择后显示名称] [隐藏:customer_id]

金额:[正常形式的东西]

模态形式:

搜索 [ ]

[客户表,按搜索过滤] [选择此客户按钮]

我认为我不需要 [http post] 方法来捕获模态表单的选择 - 相反,我只想使用 javascript 来更新发票表单的客户 ID(隐藏)和客户名称的值。

到目前为止,我有:

控制器:

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

模态部分:

-- 还没有实现搜索 - 现在尝试从数字文本框插入工作

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

发票视图

@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

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

目前代码显示表单,按钮正确打开带有表单的模态。

提前致谢!我一直在四处寻找,但没有看到任何关于从模式插入回“背景页面”的信息。 (而且我用的 AJAX 不多)。

使用:

  • Visual Studio 2015
  • ASP.NET MVC 实体框架
  • Bootstrap(尝试使用 TwitterBootstrapMVC 没有任何运气)

【问题讨论】:

  • 考虑使用自动完成控件(例如jQueru-ui Autocomplete
  • 研究了一下——非常酷。尝试通过 Nuget 包管理器添加它,看起来他们最近没有发布更新,所以它不兼容。 '错误无法找到与'bootstrap 3.3.7约束'兼容的'jQuery'版本
  • 深入研究 - 原来 jQuery.UI.Combined 得到了维护。我正在尝试获取一个简单的示例版本并努力解决它。我有超过 30k 条记录 - 这真的能够处理这么多记录的过滤吗?
  • 肯定会的。您可以使用这些选项进行 ajax 调用,该调用根据输入中的文本返回过滤后的数据(ID 和 Name 值),然后包含 ID 的隐藏输入并在 select 回调中设置。稍后我会看看是否可以找到指向好示例的链接
  • 请参阅this answer 以获取示例。您设置 source: 选项以对服务器方法进行 ajax 调用,该方法返回 JsonResult(一组匿名属性,包含由输入中的文本过滤的对象的 ID 和名称)并设置 @987654330 @ 选项使用选定的 ID 更新隐藏的输入。如果您有那么多记录,您可能希望将 minLength: 设置为 2 或 3

标签: javascript ajax asp.net-mvc forms html-helper


【解决方案1】:

您可以使用引导事件“hidden.bs.modal”或“hide.bs.modal”将选定客户的值从模态插入到后台表单。

演示

$('#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>

【讨论】:

  • 喜欢这个概念,我可以从 stackoverflow 运行代码。但是,当我将您的代码复制/粘贴到虚拟项目中时,从模式中选择“选择”不会更新主页。这是为什么?我用 包围了 js 脚本。添加console.log语句,当我关闭模式时,js似乎没有运行。有任何想法吗?谢谢! (同样,对于 js 来说还是很新的。)
  • 搞定了!我将脚本更改为: $(document).on('hidden.bs.modal', '#myModal', function () { document.getElementById("customerName").value = document.getElementById("searchCustomer") .value; })
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
  • 2012-09-11
相关资源
最近更新 更多