Note that there are some explanatory texts on larger screens.

plurals
  1. PODisable MVC "must be a number" data type validation for a field
    text
    copied!<p>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.</p> <p>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.</p> <p><em>Read below for code snippets</em></p> <p>I've read that the ComboBox does allow you to enter in values that are not in the list, and used the demo (<a href="http://demos.telerik.com/aspnet-mvc/combobox/servervalidation" rel="nofollow">Here</a>)</p> <p>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.</p> <p><strong>I need a way to try and suppress this validation, just for the LocationID field, but still protect against null values.</strong></p> <pre>BEGIN EDITS</pre> <p><strong>EDIT:</strong> I did find <a href="http://www.go4answers.com/Example/client-side-validation-suppressing-167251.aspx" rel="nofollow">this post</a>, but as the OP there says, the javascript hack is not very extensible, so I really want to avoid it.</p> <p><strong>EDIT:</strong></p> <p>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.</p> <p>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 <em>window.mvcClientValidationmetadata</em> is somehow cleared when it reaches this event, even though the metadata are properly pushed in originally.</p> <p>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).</p> <pre>END EDITS</pre> <p>Additionally, right now I'm binding to my model directly like so:</p> <pre><code> public JsonResult Create(MyEntity Model) </code></pre> <p>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. </p> <p>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.</p> <p>Thanks!</p> <p><strong>Code Snippets</strong></p> <p><strong>Model:</strong></p> <pre><code>class IntegerValueList { public Int16 ID { get; set; } public string Name { get; set; } } var lists = new Dictionary&lt;string, IEnumerable&lt;object&gt;&gt;(); lists["Locations"] = (from record in db.Locations orderby record.Name select new IntegerValueList { ID = record.LocationID, Name = record.Name }).ToList(); </code></pre> <p><strong>Controller:</strong></p> <pre><code> LocationList = new SelectList(lists["Locations"], "ID", "Name", LocationID); </code></pre> <p><strong>View:</strong></p> <pre><code> &lt;td&gt; &lt;div class="editor-field"&gt; &lt;%: Html.Telerik().ComboBoxFor(model =&gt; model.LocationID) .BindTo(Model.LocationList) .Filterable(c =&gt; c.FilterMode(AutoCompleteFilterMode.Contains)) %&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.LocationID, "*") %&gt; &lt;/div&gt; &lt;/td&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload