【发布时间】:2019-03-11 05:55:48
【问题描述】:
我想设置单击下拉列表时会丢失的默认值。我希望无法选择“请选择”值。当我单击 materialId 或 depotId 中的“请选择”值时,“”空值由 ajax 发送,我收到错误。我怎样才能防止这种情况发生?
创建.cshtml
<div class="form-group">
@Html.LabelFor(model => model.materialId, "Material names", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("materialId", null, "Please select", htmlAttributes: new { @class = "form-control chosen" })
@Html.ValidationMessageFor(model => model.materialId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.depotId, "Product Outlet", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("depotId", null, "Please select", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.depotId, "", new { @class = "text-danger" })
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('#depotId').change(function () { sendDataByAjax(); });
$('#materialId').change(function () { sendDataByAjax(); });
})
function sendDataByAjax() {
var materialId= $('#materialId option:selected').val();
var depotId= $('#depotId option:selected').val();
if (materialId == "" || depotId == "") {
// I can not write this
}
$.ajax({
type: "GET",
url: "@Url.Action("GetStock", "OutgoingProduct")",
data: {
'materialId': materialId,
'depotId': depotId
},
success: function (data) {
$("#stock").html(data);
}
});
}
</script>
}
当“”转到我的控制器时,我在这里遇到错误。因为它不是int。
OutgoingProductController.cs
public string GetStock(string materialId, string depotId)
{
int did = int.Parse(depotId);
int mid = int.Parse(materialId);
【问题讨论】:
-
当占位符文本项被选中时,
materialId和depotId的值是多少?他们是否被分配了undefined(使用console.log()来查看他们)? -
当我从列表中选择时,我从 materialId 和 depotId 中获取数字,例如“1050”、“3”。但是当我选择“请选择”值时,我得到的是空值。
-
你为什么不改变你的if条件来检查
null而不是""? -
@büşratabak,那么当您选择“请选择”时,您期望哪些值会返回?
-
当然,我尝试了 null 而不是 ""。不工作
标签: c# html asp.net asp.net-mvc