[英]I want to set default value that is missing when click on the dropdown list.I would like to be unable to select “Please select” value
I want to set default value which will miss when click on the dropdown list.I would like to be unable to select "Please select" value. 我想设置单击下拉列表时会丢失的默认值。我希望无法选择“请选择”值。 When I click on "Please select" value in materialId or depotId, "" null value send by ajax and I am getting error. 当我在materialId或depotId中单击“请选择”值时,ajax发送的“”空值会出现错误。 How can I prevent this? 我该如何预防?
Create.cshtml Create.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>
}
I am getting error here when "" goes to my controller. 当“”转到我的控制器时,这里出现错误。 Because it is not int. 因为它不是int。
OutgoingProductController.cs OutgoingProductController.cs
public string GetStock(string materialId, string depotId)
{
int did = int.Parse(depotId);
int mid = int.Parse(materialId);
Try returning false
when materialId == "" || depotId == ""
当materialId == "" || depotId == ""
时尝试返回false
materialId == "" || depotId == ""
: materialId == "" || depotId == ""
:
function sendDataByAjax() {
var materialId= $('#materialId option:selected').val();
var depotId= $('#depotId option:selected').val();
if (materialId == "" || depotId == "") {
return false;
}
$.ajax({
type: "GET",
url: "@Url.Action("GetStock", "OutgoingProduct")",
data: {
'materialId': materialId,
'depotId': depotId
},
success: function (data) {
$("#stock").html(data);
}
});
}
Since your dropdownlist selected values are passed as numeric values, you may use parseInt()
function to try parsing numeric value from client-side and then check against NaN
, if the value is numeric then trigger AJAX callback: 由于下拉列表中选择的值作为数字值传递,因此您可以使用parseInt()
函数尝试从客户端解析数字值,然后检查NaN
,如果该值是数字,则触发AJAX回调:
function sendDataByAjax() {
var materialId = parseInt($('#materialId option:selected').val());
var depotId = parseInt($('#depotId option:selected').val());
if (isNaN(materialId) || isNaN(depotId)) {
// do something, e.g. alert user
return false;
}
else {
$.ajax({
type: "GET",
url: "@Url.Action("GetStock", "OutgoingProduct")",
data: {
'materialId': materialId,
'depotId': depotId
},
success: function (data) {
$("#stock").html(data);
}
});
}
}
Then make sure that your action method contains int
type for both parameters, hence no need to use int.Parse()
which will throwing exception if parsed string has null value: 然后确保您的操作方法的两个参数都包含int
类型,因此无需使用int.Parse()
,如果已分析的字符串具有null值,则将引发异常:
public ActionResult GetStock(int materialId, int depotId)
{
int did = depotId;
int mid = materialId;
// other stuff
}
the way to solve this is not the way you're trying to do it. 解决此问题的方法不是您尝试执行的方法。 the way to do should be to use the validation system, available for MVC. 这样做的方法应该是使用可用于MVC的验证系统。
Change your get method to use a model, something like: 更改get方法以使用模型,例如:
public class StockRequestModel
{
[Required]
public int materialId { get; set }
[Required]
public int depoId { get;set; }
}
Your controller method can then become something like: 然后,您的控制器方法可以变为:
public string GetStock([FromUri] StockRequestModel model)
{
if ( !ModelState.IsValid )
{
some code here
}
//at this point your model is valid and you have IDs so can proceed with your code
}
Normally, in MVC you would return the original View with the state in the result, so you can then trigger the front end validation. 通常,在MVC中,您将返回带有结果状态的原始View,因此您可以触发前端验证。 In your case, you seem to have a WebAPI controller in an MVC app, but you can still use front end validation. 就您而言,您似乎在MVC应用程序中具有WebAPI控制器,但仍可以使用前端验证。
There are other questions related to yours, such as Client side validation with Angularjs and Server side validation with Asp.net MVC 还有其他与您有关的问题,例如使用Angularjs进行客户端验证和使用Asp.net MVC进行服务器端验证
Normally I'd vote to close it as duplicate, but in this case, I think it's worthwhile pointing out the issue and solution. 通常,我会投票决定将其重复关闭,但在这种情况下,我认为值得指出问题和解决方案。
Another place to go would be https://angularjs.org/ and then check the Form Validation section for pure front end validation. 另一个可以去的地方是https://angularjs.org/ ,然后检查“表单验证”部分中的纯前端验证。 Of course you'd want to keep both front end and back end validations. 当然,您希望同时保留前端和后端验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.