禁用 MVC“必须是数字”字段的数据类型验证

发布于 2024-10-29 05:11:12 字数 2645 浏览 7 评论 0原文

我有一个 Telerik MVC ComboBox,其中包含位置列表。客户希望最终用户能够直接将新位置输入到列表中。

提交表单后,它应该接受一个新值并插入到位置表中,当然还要更新要添加的记录的 LocationID 到新插入位置的 ID。

阅读下面的代码片段

我已经读到,组合框确实允许您输入列表中没有的值,并使用了演示(这里)

保存位置的代码,编辑locationID不是问题。我的问题是我的组合框包含整数/字符串值对的列表,而不是字符串/字符串。因此,我的代码中遇到的问题是,如果我尝试提交新的位置名称,它会尝试验证它并说它不是数字。

我需要一种方法来尝试抑制此验证,仅针对 LocationID 字段,但仍然可以防止空值。

BEGIN EDITS

编辑: 我确实找到了 这篇文章,但正如OP那里所说,javascript hack不是很可扩展,所以我真的想避免它。

编辑:

我最终使用了 javascript hack,这是我发现的所有有效方法。我计划将其封装在方法或属性中,并将其作为答案发布。

我发现,通过此 hack,如果我使用 Telerik().ScriptRegistrar().OnDocumentReady()(正确的 window.mvcClientValidationmetadata 在到达此事件时会以某种方式清除,即使元数据最初已正确推送。

为了解决这个问题,我必须手动将代码放在表单结束标记下方的自己的脚本块中(这是呈现客户端验证数组的位置)。

END EDITS

另外,现在我直接绑定到我的模型,如下所示:

        public JsonResult Create(MyEntity Model)

我不确定当进行模型绑定时会如何解决,我猜它可能只是返回一个错误,我不会'甚至没有达到我的操作方法代码。

所以我想这里的想法是在方法签名中使用 FormCollection,检测非整数 LocationID,插入更新,然后运行 ​​UpdateModel()?当然,欢迎提出更好的建议。

谢谢!

代码片段

模型:

class IntegerValueList
{
    public Int16 ID { get; set; }
    public string Name { get; set; }
}

        var lists = new Dictionary<string, IEnumerable<object>>();

        lists["Locations"] = (from record in db.Locations
                              orderby record.Name
                              select new IntegerValueList
                              {
                                  ID = record.LocationID,
                                  Name = record.Name
                              }).ToList();

控制器:

        LocationList = new SelectList(lists["Locations"], "ID", "Name", LocationID);

视图:

                <td>
                    <div class="editor-field">
                        <%: Html.Telerik().ComboBoxFor(model => model.LocationID)
                                .BindTo(Model.LocationList)
                                .Filterable(c => c.FilterMode(AutoCompleteFilterMode.Contains)) 
                        %>

                        <%: Html.ValidationMessageFor(model => model.LocationID, "*") %>
                    </div>
                </td>

I have a Telerik MVC ComboBox which contains a list of locations. The client wants the end user to be able to directly enter new locations into the list.

Upon submitting the form, it should accept a new value and insert into the locations table, and of course update the LocationID of the record being added to the ID of the newly inserted location.

Read below for code snippets

I've read that the ComboBox does allow you to enter in values that are not in the list, and used the demo (Here)

Code to save location, edit locationID is not a problem. My problem here is that my Combo box contains a list of integer/string value pairs, not string/string. And thus, the issue I have in my code is that if I try and submit a new location name, it tries to validate it and says its not a number.

I need a way to try and suppress this validation, just for the LocationID field, but still protect against null values.

BEGIN EDITS

EDIT: I did find this post, but as the OP there says, the javascript hack is not very extensible, so I really want to avoid it.

EDIT:

I ended up using the javascript hack, its all I've found which works. I plan to encapsulate this in a method or attribute, and post it as an answer.

I found that with this hack, it did not work in Firefox or Chrome if I place the code block inside the document ready event using Telerik().ScriptRegistrar().OnDocumentReady(), the properly window.mvcClientValidationmetadata is somehow cleared when it reaches this event, even though the metadata are properly pushed in originally.

To get around this, I had to manually put the code in its own script block just under the form closing tag (which is where the client-side validation array is rendered).

END EDITS

Additionally, right now I'm binding to my model directly like so:

        public JsonResult Create(MyEntity Model)

I'm not sure how that's going to work out when it comes time to do model binding, I'm guessing it may just return an error, and I won't even reach my action method code.

So I guess the idea here would be to use a FormCollection in the method signature, detect for a non-integer LocationID, insert update, and then run UpdateModel()? Of course, welcome to better suggestions.

Thanks!

Code Snippets

Model:

class IntegerValueList
{
    public Int16 ID { get; set; }
    public string Name { get; set; }
}

        var lists = new Dictionary<string, IEnumerable<object>>();

        lists["Locations"] = (from record in db.Locations
                              orderby record.Name
                              select new IntegerValueList
                              {
                                  ID = record.LocationID,
                                  Name = record.Name
                              }).ToList();

Controller:

        LocationList = new SelectList(lists["Locations"], "ID", "Name", LocationID);

View:

                <td>
                    <div class="editor-field">
                        <%: Html.Telerik().ComboBoxFor(model => model.LocationID)
                                .BindTo(Model.LocationList)
                                .Filterable(c => c.FilterMode(AutoCompleteFilterMode.Contains)) 
                        %>

                        <%: Html.ValidationMessageFor(model => model.LocationID, "*") %>
                    </div>
                </td>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯了 2024-11-05 05:11:12

当我过去不得不这样做时,我在该字段中使用了一个带有“输入位置”之类标签的文本框,然后在其下方使用了一个带有“或选择...”之类标签的下拉菜单,然后使用用于填充文本框的下拉列表的 onChange 事件。现在您没有验证下拉列表。

您甚至可以在文本框旁边放置一个“选择”链接,并隐藏下拉列表,直到单击该链接。缺点是我们最终可能会将圣地亚哥、圣地亚哥和圣地亚哥全部输入到位置列表中……但是您的客户的要求不允许我们明确阻止这种情况。

When I've had to do this in the past, I have used a textbox for the field with a label like "Enter Location", and then a dropdown below it with a label like "Or select...", and then use the onChange event of the dropdown to populate the textbox. Now you are not validating the dropdown.

You could even put a "Select" link next to the textbox and have the dropdown hidden until the link is clicked. The drawbback is that we could end up with San Diego, SanDiego and Sandiego all entered in the location list...but then your customer's requirement doesn't let us explicitly prevent that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文