繁体   English   中英

我想设置单击下拉列表时缺少的默认值。我希望无法选择“请选择”值

[英]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

我想设置单击下拉列表时会丢失的默认值。我希望无法选择“请选择”值。 当我在materialId或depotId中单击“请选择”值时,ajax发送的“”空值会出现错误。 我该如何预防?

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>
}

当“”转到我的控制器时,这里出现错误。 因为它不是int。

OutgoingProductController.cs

public string GetStock(string materialId, string depotId)
{
    int did = int.Parse(depotId);
    int mid = int.Parse(materialId);

materialId == "" || depotId == ""时尝试返回false 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);
                }
        });
}

由于下拉列表中选择的值作为数字值传递,因此您可以使用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);
            }
        });
    }
}

然后确保您的操作方法的两个参数都包含int类型,因此无需使用int.Parse() ,如果已分析的字符串具有null值,则将引发异常:

public ActionResult GetStock(int materialId, int depotId)
{
    int did = depotId;
    int mid = materialId;

    // other stuff
}

解决此问题的方法不是您尝试执行的方法。 这样做的方法应该是使用可用于MVC的验证系统。

更改get方法以使用模型,例如:

public class StockRequestModel
{
    [Required]    
    public int materialId { get; set }

    [Required]
    public int depoId { get;set; }
}

然后,您的控制器方法可以变为:

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
}

通常,在MVC中,您将返回带有结果状态的原始View,因此您可以触发前端验证。 就您而言,您似乎在MVC应用程序中具有WebAPI控制器,但仍可以使用前端验证。

还有其他与您有关的问题,例如使用Angularjs进行客户端验证和使用Asp.net MVC进行服务器端验证

通常,我会投票决定将其重复关闭,但在这种情况下,我认为值得指出问题和解决方案。

另一个可以去的地方是https://angularjs.org/ ,然后检查“表单验证”部分中的纯前端验证。 当然,您希望同时保留前端和后端验证。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM