kendo ui dropdownlist with html5 required attribute - asp.net-mvc

I'm using kendo ui dropdownlist component (not the wrapper) in my Asp.net mvc 4 project.
We are not using kendo ui validator.
I want to use html5 required attribute for this field. But when I set required attribute, when no value is selected, it does not give any message, also does not submit. But when I use simple (select tag):
<select required="required">
<option></option>
...
</select>
It works; When I not select a value, it does not submit and gives an error message (html5 required attribute).
Is it possible that for kendo ui dropdownlist behaves exactly like normal html select component.

Yes it is possible.
You can add a select event to the kendo dropdown and check if any value is selected or not. If no value is selected you can raise a warning before submitting the form.

Related

MVC6 select tag helper for edit applying current value

I have a viewmodel with a state dropdownlist.
The form is being populated from a database and I everything works except I dont know how to apply the current value for state as the default value for the select helper tag?
This is the CSHTML for the select:
<select asp-for="State" asp-items="#Model.StatesList" > </select>
This provides the all the states when you do a dropdown however is there a way to set the value to the current value for Model eg #Model.state so if you dont select any new dropdown value it takes the original value for the record?

kendo grid in inline editing mode with Dropdownlist required field

I have a kendo grid in inline editing mode. When I press "Add new item" button, new row is added. some editable fields are ForeignKey column in the grid, if i fill the text fields in the new records without filling or select the dropdownlist fields for ForeignKey columns, a null values are set for ForeignKey fields.
the question , how to enforce the user to select dropdownlist and make this field is required on "update" button click action.
i am using ASP.NET MVC 4.0 and Kendo UI for ASP.NET MVC Q2 2013.
Make sure your dropdownlists are binded to non nullable values, and are you using the EditorTemplate provided for the foreign key columns?

Asp.NET MVC 2 dynamic editor template based on dropdown value

I am working on a model criteria builder. I have a dropdown list with all of the model properties, a dropdown list with static operators (less than, equals, like, etc) and a user input form element for the value. The issue is that the form element type (dropdown, date, text box, etc) for the user input value needs to be based on the data type of the model property chosen in the first dropdown list. What is the best way to achieve this using MVC 2? Ideally I would like to just create an Html extension method and use it like Html.CriteriaFilterFor(model => model) and be able to customize the display using model attributes and metadata.
You should use JQuery to populate the other one. An AJAX call would allow you to pull the second drop down's list. Populating Dropdownlist Using MVC2 Based On Another Dropdownlist (Cascading DropDownList)

Required Attribute for bool value type when used as checkbox

I'm running into a problem where I have a simple add/edit form and using client-side validation (jQuery/MicrosoftMvcJQueryValidation.js) which is generated from data annotations and enabled client side by calling the following in my view:
<% Html.EnableClientValidation(); %>
This seems to work fine for most elements, however I have an instance where I have a boolean property which is rendered as a checkbox using:
<%= Html.EditorFor(model => model.Chargeable)%>
Which can be either true/false (ticked/unticked).
As the bool is a value type, and not nullable, it is being rendered as a required property and displays an error (client side) when the form is submitted reading "The Chargeable field is required.", however, as the HTML that is generated is two part (both checkbox and hidden value) it will pass the post back validation.
After browsing the MVC 2 source code, I've managed to put a "quick and dirty" fix in for the moment, which is to set:
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
Any one else got any ideas or suggestions on how I can get around this?
IMO, I dont think MVC should be setting the client-side required validator for checkboxes rendered using the Html.EditorFor/Html.CheckBox(For) methods.
Note: I'm using the ASP.NET MVC 2 RC2 and the MicrosoftMvcJQueryValidation.js from the matching MVC Futures release.
I suppose the easiest way of handling it is to call the rules("remove", [rules]) function on the elements (mainly checkboxes) that I want to remove the client-side validation from:
<script type="text/javascript">
$(document).ready(function() {
$('#Chargeable').rules('remove', 'required');
});
</script>

ASP.NET MVC how to handle the default empty value for dropdown list

I create a dropdown list by using ASP.NET MVC helper like this,
<%=Html.DropDownList("Group","-please select a group-")%>
and I get the html like this,
<label for="GroupId">Group:</label>
<select id="GroupId" name="GroupId"><option value="">-please select a group-</option>
<option value="15">Business</option>
<option value="16">Friends</option>
<option value="17">Others</option>
</select>
The default option is "-please selecta group-" and the value is empty.
How can I validate the select value to see if it's empty?
I mean if user doesn't choose a group, how can I know it and give user a error message.
Now, the code only shows exception error, because the value is empty for the default option.
The value of Group in the Action will be 0.
You could test if Group is zero and call ModelState.AddModelError.
It would be better, if it was possible to explicitly set the value of the default item in the dropdownlist, but that is not possible in the Html.DropDownList
Anyway I use the overload:
With that you have one Property in the Model for the list of Groups and one for the key of the selected Group. If I use
then ModelState.IsValid is always false for me.

Resources