简体   繁体   English

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

[英]Disable MVC “must be a number” data type validation for a field

I have a Telerik MVC ComboBox which contains a list of locations. 我有一个Telerik MVC组合框,其中包含位置列表。 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. 提交表单后,它应该接受一个新值并将其插入到locations表中,并且当然要更新要添加到新插入位置的ID中的记录的LocationID。

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 ) 我读过ComboBox确实允许您输入列表中未包含的值,并使用了演示( 此处

Code to save location, edit locationID is not a problem. 保存位置的代码,编辑locationID没问题。 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. 我需要一种方法来仅针对LocationID字段尝试抑制此验证,但仍要防止出现空值。

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. 编辑:我确实找到了这篇文章 ,但是正如OP所说的那样,javascript hack不是非常可扩展的,所以我真的想避免它。

EDIT: 编辑:

I ended up using the javascript hack, its all I've found which works. 我最终使用了JavaScript hack,发现了所有可用的方法。 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. 我发现使用此黑客,如果我使用Telerik()。ScriptRegistrar()。OnDocumentReady()将代码块放置在文档就绪事件中,则它在Firefox或Chrome中不起作用,正确的window.mvcClientValidationmetadata到达时会以某种方式清除即使元数据最初已正确推送,也会发生此事件。

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()? 所以我想这里的想法是在方法签名中使用FormCollection,检测非整数的LocationID,插入更新,然后运行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>

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. 过去,当我不得不执行此操作时,我为该字段使用了一个文本框,其标签为“输入位置”,然后在其下方的下拉菜单中为标签为“或选择...”,然后使用下拉列表的onChange事件以填充文本框。 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. 缺点是我们最终可能将San Diego,SanDiego和Sandiego都输入到位置列表中...但是,那么您客户的要求并不能使我们明确地避免这种情况。

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

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