繁体   English   中英

针对 ASP.Net-MVC3 中的 object 的客户端验证?

[英]Client-side validation against an object in ASP.Net-MVC3?

我有啊 HTML5 表格,动作定义如下:

@using (Html.BeginForm("SearchAction", "ResultsController"))

该表单包含两个文本字段:

<input type="text" name="txtSearchTerm" id="txtSearchTerm" class="frontPageInput" placeholder="Begin your search..." required />          
<input type="text" name="txtGeoLocation" id="txtGeoLocation"  class="frontPageInput" required />          

The txtGeoLocation field is an autocomplete field that is fed from a cached object, fed through the controller and by a model repository class through the following jQuery code:

<script type="text/javascript" language="javascript">
    $(function () {
        $("#txtGeoLocation").autocomplete(txtGeoLocation, {
            source: function (request, response) {
                $.ajax({
                    url: "/home/FindLocations", type: "POST",
                    dataType: "json",
                    selectFirst: true,
                    autoFill: true,
                    mustMatch: true,
                    data: { searchText: request.term, maxResults: 10 },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { label: item.GeoDisplay, value: item.GeoDisplay, id: item.GeoID }
                        }))
                    }
                })
            },
            select: function (event, ui) {
                alert(ui.item ? ("You picked '" + ui.item.label + "' with an ID of " + ui.item.id)
                    : "Nothing selected, input was " + this.value);
                document.getElementById("hidLocation").value = ui.item.id;
            }
        });

    });

那里有一个用于调试的警报。 单击下拉的文本时,会触发此警报,但是如果您输入整个单词并点击提交,它不会触发。

我想首先在客户端验证地理文本框中的文本,以确保它是集合中包含的值,而不是,让文本框为红色,传达这一点。

谢谢。

您可以使用 [Remote()] 属性使用 jquery 远程验证来验证值是否在列表中。 当您回发时,您也必须在服务器端进行相同的检查。

暂无
暂无

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

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