Kendo Combobox - reset value when the selected value is not in the datasource - user-interface

We were using Telerik MVC extensions for ASP.NET MVC and have started migrating to Kendo UI now.
I am facing an issue with Kendo Combo and the details are as follows:
Suppose that I am binding a Kendo combo with a list of ten items (whose values are from 1 to 10 along with a blank item (say "Select" and it's value is "")) and in my markup, if I say .Value("0") - I don't want the kendo combo to show me "0" but instead get reset to the blank item or may be the first item in the list.
How can I achieve this ?
Regards

Your int has to be a nullable type.
ex:
public class SomeDTO
{
public int? ID {get; set;} //must be type int? and not int
public string Name {get; set;}
}
// on your kendo binding, .DataValueField('ID')

Related

How to select multiple items from dropdown list, save it and again repopulate the dropdown with the selected list?

I am working on an ASP.NET MVC3 project. I am facing issues regarding multiple selection in dropdown.
The problem is, I have to save multiple items in database from a dropdown list and repopulate it.
I have used the class below to represent each list data:
public class IDNameValueTO {
public int ID { get; set; } //Value of the selection Element
public string Name { get; set; } //Name of the selectionElement
public int Value { get; set; } //1 if value is checked and 0 if not
}
My List goes as follows:
public List<IDNameValueTO> tempList = new List<IDNameValueTO>();
ViewBag.SelectedList = tempList;
I am generating the dropdown list as follows:
#Html.DropDownList("SelectedValue", new SelectList(ViewBag.SelectedList, "ID", "Name"))
Now how do I save the multiple selection and display it later on using dropdown?
The default DropDown helper that is provide in asp.net mvc does not support multiselet.
You'll have to create your own custom multiselect dropdown helper.
Here are some links that can help you create you own custom dropdown helper:
http://blogs.msdn.com/b/rickandy/archive/2012/01/30/asp-net-mvc-dropdownlist-multiselect-and-jquery.aspx
http://utsavized.com/chosen-multiselect-dropdown-list-with-asp-net-mvc3/

Foreign key column sorting

I'm using Kendo grid with ForeignKey column with sorting. By default this column is sorted by value but we need it to be sorted by text. Could anyone provide please an example of doing it using ASP.NET Wrappers?
I found the trick was to implement IComparable on the foreign key object, which then sorts by the text name instead of the id in the Kendo grid:
public class MyForeignKeyModel : IComparable<MyForeignKeyModel>
{
public int ID { get; set;}
public string Name { get; set;}
public int CompareTo(MyForeignKeyModel compareTo)
{
return String.Compare(Name, compareTo.Name, StringComparison.InvariantCulture);
}
}
All the other solutions mentioned by users and Telerik look much more complicated!
Reponse by Atanas Korchev (Admin, Kendo UI) We can’t support this in all cases because the data source won’t have all data (it usually has just the foreign key which is the value)
You can use Grouping if that helps to some extend.

MVC3 DropDownListFor not setting selected property

In MVC3, when using DropDownListFor is it necessary for the first parameter to be a string? I have the following setup:
#Html.DropDownListFor(m => m.MyListItemId, Model.MyListItems,
new Dictionary<string, object>
{
{ "style", "width:120px" },
{ "data-type", "myList" }
})
where m.MyId is an int on my viewmodel. I'm having an issue where when I change the selected item in my drop down list, and inspect the rendered html, the "selected" property is not set to the newly selected item. This is a problem as i'm using jquery clone function to copy that row and i need the list with the new selected item to be copied to my new row. Ideas?
Update - Changing the property on the viewmodel to a string makes no difference.
Is this a bug with mvc dropdownlistfor? I've read quite a few posts on similar issues, but can't seem to find a solution that works in this instance. This is how my list is setup in my code:
var myListItems = _myRepository.GetAll();
model.MyListItems = new SelectList(myListItems, "Id", "Name", lineItem.myListItemId);
model.MyListItemId = lineItem.myListItemId;
where lineItem is passed into this method
No, the selected value property does not need to be a string, it can be an int. As long as the value is convertible to a string, it should work (so selected value type could be a Guid, int, bool, etc).
I have sometimes found issues when my route for the page has a route parameter with the same name as the selected value model property. For example, consider this:
route: "/establishments/{establishmentId}/edit"
Model:
public class MyViewModel
{
public int EstablishmentId { get; set; }
public SelectListItem[] Establishments { get; set; }
}
View:
#Html.DropDownListFor(m => m.EstablishmentId, Model.Establishments)
With this code, the selected value of the drop down list would always be whatever establishmentId is in the route. So if the route were /establishments/12/edit, then value 12 would be selected in the dropdown. It doesn't matter that the route parameter and model property capitalization doesn't match.
I figured this out by downloading the MVC source code, making my own copy of the DropDownListFor (named MyDropDownListFor), then stepping through the code to see what happened. If you are still having trouble with MVC3, I suggest you do the same. You need to figure out whether this is an issue with the server code, or your jquery clone stuff.

DropDownList Client Side Validation is validating when it should not be. (MVC3, Razor)

I am still learning MVC3 and Razor, so this is perhaps a simple question.
On a view I have a DropDownList whose sole purpose is to help filter (via AJAX) a second drop down list:
#Html.DropDownList("Segments", "-- select segment --")
There is a Segments property of the ViewModel that is defined as:
public IEnumerable<SelectListItem> Segments { get; set; }
There is JavaScript that handles the change event for this DropDownList and populates another DropDownList with appropriate values. That other DropDownList is defined like this:
#Html.DropDownListFor(m => m.fafhProdRecId, Enumerable.Empty<SelectListItem>(), "-- select product recommendation --")
This all works fine until I submit. When I submit, I get a validation error on the Segments drop down list!
Now -- there should be absolutely NO validation on the segments DropDownList -- there shouldn't be any client side validation on EITHER drop down list, for that matter.
But when I try to submit, I get the validation error message back:
The value '1' is invalid.
I have no idea why this is happening.
I have no idea how to decorate the Segments property to say that it is NOT required.
I have no idea how to tell the unobtrusive javascript validator that it is, in fact, being quite obtrusive.
In your ViewModel class add [Bind(Exclude = "Segments")]
From: Using Data Annotations for Model Validation
make sure that your Model has fafhProdRecId as nullable, I imagine it's declared as:
public int fafhProdRecId { get; set; }
change this to:
public int? fafhProdRecId { get; set; }
hopefully, that should resolve the issue as this effectively makes the model field nullable (assuming the db field IS nullable too of course).

Listboxfor MVC3 Razor. User selection from database fields

I'm working with a Razor MVC3 project
I have a model
public class ProductFormatModel
{
public int ID {get; set;}
public string SerialID { get; set; }
public ProductTypes ProductType { get; set; }
public List<ProductComponent> Components { get; set; }
}
The ProductComponent class has two properties: int ID and string Description.
I would like the controller to work with a strongly typed view of ProductFormatModel type to create a new ProductFormatModel. The Create view should look like this:
A textfield to insert SerialID (done)
A dropdownlist to select the value of the enumerator ProductType (done)
A listbox with different rows showing the "Description" property of ProductComponent. Every row should present a record in the database. The user must be able to select one or more rows and in this way the property "Components" should have those objects in the list.
The database is a simple DbContext. MyDB.Components gives me the recordset of table "Components"
How do I pass the values of the recordset from the controller to the view?
Should I call a listboxfor with multiselectlist?
Should I add another property to the model like List ComponentsToBeSelected in which I can pass the list?
One possible design:
Use two listboxes in your view. Load the first with all possible ProductComponents that are not in model.Components, and use ListBoxFor to bind the second to the model.Components list.
Place two buttons, Add and Remove, between the lists. Wire these up with jQuery to move items between the two listboxes.

Resources