【发布时间】: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