asp.net-core - ASP.NET Razor 页面中的可选查询字符串参数
问题描述
在我的高级搜索表单中,我有大约 25 个字段。当我提交表单时,所有参数都会传递给 URL,即使为 null。如何传递仅由用户更改并具有值的参数?所有 25 个字段都是可选的。
我有大约 25 个:
[BindProperty(SupportsGet = true)]
[Display(Name = "Foo")]
public int? Foo{ get; set; }
OnGetAsync:
public async Task<IActionResult> OnGetAsync()
{
Properties = await _DarkMatterRepo.FindAsync(Foo, ...)
return Page();
}
解决方案
如何传递仅由用户更改并具有值的参数?
要实现上述要求,您可以尝试检查表单数据并导航到预期页面,如下所示。
形式代码
<form method="get">
<input type="text" name="name">
<input type="email" name="emailaddress">
<input type="number" name="age">
@*other input fields*@
<input type="submit">
</form>
动态生成 QueryString 并导航到特定页面
<script>
$( "form" ).on( "submit", function( event ) {
event.preventDefault();
//console.log($(this).serializeArray());
var formdata = $(this).serializeArray();
var parm = "";
$.each(formdata, function (index, item) {
if (item.value!="") {
parm += item.name + "=" + item.value + "&";
}
});
//console.log(parm.substring(0, parm.length - 1));
window.location.href = "@Url.Page("Index")" + "?" + parm.substring(0, parm.length - 1);
});
</script>
推荐阅读
- php - 如何将数据从android发送到mysql?
- r - 用 3 列创建一个新列
- html - css:悬停效果覆盖的高度比引导导航栏中的文本更大
- c# - 在 Visual Studio Code for Unity 中安装 Nuget 包
- c++ - 如果 A(“整体”)有一组 B(“部分”),如何将“部分”B 从“整体”A 转移到另一个?
- python - 无法设置 tensorflow gpu 对象检测
- mysql - 选择包含所有指示值的 ID(通配符冲突问题)MariaDB
- java - oneToMany 关系中的 HQL 查询
- php - 谷歌电子表格值获取方法中的错误 500
- javascript - setstate 没有将值设置为状态