【问题标题】:I want to set max value(with id and coming from ajax) into Html.EditorFor()我想将最大值(带有 id 并来自 ajax)设置为 Html.EditorFor()
【发布时间】:2019-07-25 09:52:27
【问题描述】:

在我从下拉列表中选择后,股票的价值是通过 ajax 来的,我希望用户不能写入大于该股票价值的金额值。我无法将该最大值添加到 html.editorfor

创建.cshtml

         <div class="col-md-10">
                    @Html.EditorFor(model => model.amount, new { htmlAttributes = new { @class = "form-control",min="1", max=$("#stock")} })
                </div>

   <script type="text/javascript">
    $(document).ready(function () {
        $('#depotId').change(function () { sendDataByAjax(); });
    })

    function sendDataByAjax() 
        var materialId= $('#materialId option:selected').val();
        var depotId= $('#depotId option:selected').val();

        if (!materialId || !depoId) {
        }
        $.ajax({
            type: "GET",
            url: "@Url.Action("GetStock", "OutgoingProduct")",
            data: {
                'materialId': materialId,
                'depotId': depotId
            },
            success: function (data) {
                $("#stock").html(data);

            }
        });
    }
    </script>

【问题讨论】:

    标签: html asp.net ajax asp.net-mvc model-view-controller


    【解决方案1】:

    仅使用像$("#stock") 这样的jQuery 选择器来设置&lt;input&gt; 元素的属性值不是正确的方法。我认为您应该首先定义没有最大值的 EditorFor 助手:

    @Html.EditorFor(model => model.amount, new { htmlAttributes = new { @class = "form-control", min="1" }})
    

    然后从客户端的下拉列表中处理changeonchange 事件,以将max 属性添加或设置到&lt;input&gt; 生成的&lt;input&gt; 元素EditorFor

    $("#dropdownId").change(function () {
        var value = $(this).val();
    
        // other stuff
    
        // if the data comes as single value response, just set it to input element with 'prop()'
        $.get('@Url.Action("TargetActionName", "TargetControllerName")', { id: value }, function (data) {
           // set maximum value here
           $('#amount').prop('max', data); 
        });
    });
    

    【讨论】:

    • 看不懂,"dropdownId"怎么写
    • dropdownId是你的DropDownList(For)的元素ID,你可以通过查找&lt;select id="dropdownId" ...&gt;&lt;/select&gt;找到它。您需要处理其change 事件,以便从选定的选项值中设置其他元素的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多