javascript - MVC - 我的表单没有通过我的选择下拉列表的选择选项
问题描述
我正在尝试使用 ajax 帖子将我的下拉值发布到我的控制器。除了我的下拉选择列表之外,所有表单值都可以正常传递。当我使用浏览器的 devtools 查看 select 元素时,它的名称为SortByList
.
模型
public IList<SortBy> SortByList { get; set; }
public class SortBy
{
public string Text { get; set; }
public bool Selected { get; set; }
}
查看下拉列表
@using (Html.BeginForm("FDCLSubmit", "Reports", FormMethod.Post, new { id =
"reportForm", @class = "report-form col-9" }))
{
<div class="row">
<b class="col-2" style="padding-top: 5px;">Sort By</b>
<select asp-for="SortByList" class="form-control col-3">
@for (var x = 0; x < Model.SortByList.Count; x++)
{
<option value="@Model.SortByList[x].Text">@Model.SortByList[x].Text</option>
}
</select>
</div>
(saveSubmit button partial view here)
}
阿贾克斯邮报
$('#saveSubmit').on('click', function (evt) {
var report = $('form').serialize();
var form = new Object();
form.report = report;
form.EnteredCriteriaName = $('#EnteredCriteriaName').val();
//Ajax form post
$.ajax({
type: 'POST',
data: form.report,
contentType: dataType,
url: '@Url.Action("FDCLCheckIfExists", "Reports")',
success: function (data) {
if (data.exists == true) {
//Toggle the error modal and display messages
$('#existsModal').modal('toggle');
$('#modalYes').on('click', function () {
//User selected to replace report criteria
$.ajax({
type: 'POST',
data: form.report,
contentType: dataType,
url: '@Url.Action("SaveFDCLCriteria", "Reports")',
success: function (data) {
console.log(data);
if (data.success) {
window.location.href = data.url;
} else {
//Toggle the error modal and display messages
$('#errorsModal').modal('toggle');
//Add <br> tags when there is a linebreak in the string. This will add the line breaks into the HTML.
$('#errorsModal .modal-body p').html(data.message.replace(/(?:\r\n|\r|\n)/g, '<br>'));
}
}
});
});
}
}
});
});
控制器
我在这个控制器中设置了断点,这就是SortByList
我的属性report
为空的地方。
public ActionResult FDCLCheckIfExists(FirstDollarCreditListing report, string EnteredCriteriaName)
{
var criteria = report.ConvertToCriteria(report);
criteria.CriteriaName = EnteredCriteriaName;
var rep = new ReportFirstDollarCreditListing();
var exists = rep.DoesCriteriaNameAlreadyExist(criteria);
return Json(new { exists = exists });
}
解决方案
首先,您需要知道何时使用$('form').serialize()
它会传递如下数据:
SortByList=aaa
但是模型中的 SortByList 是一个列表,它需要如下数据:
SortByList[0]=aaa
这是一个工作演示:
1.Model(需要添加一个属性来接收选中的item):
public class FirstDollarCreditListing
{
public string SelectedSort { get; set; }
public IList<SortBy> SortByList { get; set; }
}
2.查看:
注意:当您使用.serialize()
时,它会生成“查询字符串”格式的数据,需要使用默认的 contentType 发送application/x-www-form-urlencoded; charset=UTF-8
,而不是 JSON。
@model FirstDollarCreditListing
<form>
<div class="row">
<b class="col-2" style="padding-top: 5px;">Sort By</b>
<select asp-for="SelectedSort" class="form-control col-3">
@for (var x = 0; x < Model.SortByList.Count; x++)
{
<option value="@Model.SortByList[x].Text">@Model.SortByList[x].Text</option>
}
</select>
</div>
<div>
<input id="saveSubmit" type="button" value=" submit" />
</div>
</form>
<input id="EnteredCriteriaName" value="asd" />
@section Scripts
{
<script>
$('#saveSubmit').on('click', function (evt) {
var report = $('form').serialize();
var EnteredCriteriaName = $('#EnteredCriteriaName').val();
//Ajax form post
$.ajax({
type: 'POST',
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
data:report+"&EnteredCriteriaName="+EnteredCriteriaName,
url: '@Url.Action("FDCLCheckIfExists", "Home")',
success: function (data) {
//..
}
});
});
</script>
}
3.控制器:
[HttpPost]
public void FDCLCheckIfExists(FirstDollarCreditListing report, string EnteredCriteriaName)
{
//...
}
推荐阅读
- javascript - 验证时阻止日历控件打开
- javascript - 如何在我的节点 js 脚本中连接 amp 页面?
- encryption - 使用 weblogic 数据源解密 Oracle 数据库中的透明数据加密 (TDE)
- r - 如何裁剪在 R 中使用 YOLO 检测到的对象?
- qt - 错误:未知方法参数类型:QString&
- c# - 如何使用 linq 检查元素在 XML C# 中是否具有正确的子元素?
- python - 一棵树的后序遍历
- javascript - 我需要使用键值对的对象将选项下拉列表添加到选择元素
- scala - 在 Spark 的结构化流模式下,获取 Offset 的消息正在重置
- lucene - 创建索引器时文件系统锁定