asp.net-mvc - 在剃刀页面中使用数据库中的数据选择(搜索)另一个字段时自动填充字段
问题描述
我有字段,例如公司、名字、姓氏,所以当我从数据库提供的列表中选择姓氏时,我想自动填充其他字段。我怎样才能做到这一点?
动作方法:
public static IList<Adresse> GetAdresses(string lastname)
{
string sql = string.Format("SELECT * FROM Adresse WHERE
lastname like '%{0}%'", lastname);
SqlDataAdapter adapter = CreateDataAdapter(sql);
DataTable dt = new DataTable();
adapter.Fill(dt);
IList<Adresse> list = new List<Adresse>();
foreach (DataRow dr in dt.Rows)
{
Adresse item = new Adresse();
foreach (DataColumn dc in dr.Table.Columns)
{
PropertyInfo p = item.GetType().GetProperty(dc.ColumnName);
if (p != null && dr[dc] != DBNull.Value && p.CanWrite)
{
p.SetValue(item, dr[dc], null);
}
}
list.Add(item);
}
return list;
}
查看:
<div class="table_row">
<div class="table_cell" style="padding-bottom:5px;"> Firstname: </div>
<div class="table_cell" style="padding-bottom:5px;"> @Html.EditorFor(m => m.head.FirstName) </div>
</div>
<div class="table_row">
<div class="table_cell" style="padding-bottom:5px;"> LastName: </div>
<div class="table_cell" style="padding-bottom:5px;">
@Html.DropDownListFor(m => m.head.LastName, selectList, new { @class = "form-control selectpicker", @Value = @Model.head.FirstName, //onchange = "this.form.submit();" })
<script>
$(document).ready(function() {
$('.selectpicker').selectpicker({
liveSearch: true,
showSubtext: true
});
});
</script>
</div>
</div>
解决方案
由于您使用的是 jQuery,您可以向控制器中的操作发出 ajax 请求,该操作将返回地址信息。
$("#dropdownlist").change(function() {
$.ajax({
type: 'POST',
url: 'GetAddress',
data: { lastname: $("#dropdownlistID").val() },
success: function (data) {
// fill address with data
},
error: function (ex) {
console.log('Failed to retrieve data. Exception: ' + ex);
}
});
});
GetAddress()
将您的方法的签名更改为:
public ActionResult GetAddress(string lastname)
{
// your code to return address information
}
推荐阅读
- html - 带有图像的锚点的高度比其他元素的高度更高
- google-cloud-platform - gcloud 应用程序部署行为在指定/不指定存储桶的情况下有所不同
- c# - WCF 双工代理回调的 STA 线程上下文
- optimization - 需要提示!程序使用内联 asm 调用函数而不是 C 函数时出现错误行为
- python - 从一维 NumPy 数组创建一个二维掩码
- version-control - 如何为仅包含三个 yaml 脚本文件的版本构建清单文件?
- smtpclient - SmtpClient.send - 找不到路径的一部分
- java - SELECT查询在Oracle中给出唯一约束违规
- amazon-web-services - AWS EMR:解析参数时出错:预期:'=',收到:'EOF' 用于输入:
- php - 隐藏部分电话号码