This is a migrated thread and some comments may be shown as answers.

MultiSelectFor and ListView Binding

3 Answers 119 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Ant
Top achievements
Rank 1
Ant asked on 19 May 2014, 06:19 PM
Hi

I have an issue when binding both a MultiSelecr and ListView to the same properties in a ViewModel but in different views. Here is an example:

In my ViewModel I have the following public properties

//Select List of all cars
 
public IEnumerable<System.Web.Mvc.SelectListItem> AllCars
        {
            get
            {
                if (Cars != null)
                {
                    var _sacts = Cars.Select(f => new System.Web.Mvc.SelectListItem
                    {
                        Value = f.code,
                        Text = f.description
                    });
                    return _sacts;
                }
                else return null;
            }
        }
 
public IEnumerable<CarTypes> Cars { get; set; }
 
//Select List of previous saved cars
 
public IEnumerable<System.Web.Mvc.SelectListItem> SelectedCars
        {
            get
            {
                if (SelectedCarsValues != null)
                {
                    var _sacts = SelectedCarsValues.Select(f => new System.Web.Mvc.SelectListItem
                    {
                        Value = f.code,
                        Text = f.description
                    });
                    return _sacts;
                }
                else return null;
            }
        }
 
public IEnumerable<CarTypes> SelectedCarsValues { get; set; }

Cars and SelectedCarsValues are populated from a db context.

On one view i have my MultiSelect

@(Html.Kendo().MultiSelectFor(m => m.SelectedCars)
    .BindTo(Model.AllCars)
    .DataTextField("Text")
    .DataValueField("Value")
)

And on another view I have my ListView

<script type="text/x-kendo-tmpl" id="CarsTemplate">
    ${Text}
</script>
 
@(Html.Kendo().ListView(Model.SelectedCars)
    .Name("Cars")
    .TagName("p")
    .ClientTemplateId("CarsTemplate")
    .BindTo(Model.SelectedCars)
)

This all works fine. But when i submit the form containing my multiselect the validation fails (ModelState.isvalid) as it is trying to convert a selecteditem to a string. So what i did was change my ViewModel for SelectedCars to be an IEnumerable<string> instead:

public IEnumerable<string> SelectedCars
{
    get
    {
        if (SelectedCarsValues != null)
        {
            return SelectedCarsValues.Select(a => a.code);
        }
        else return null;
 
    }
}

Now when i submit my form with the multiselect the validation passes. Yet, when i use my view i now have [object][object] showing as my ListView. I can change this to ${data} but it obviously only shows the code and not the description.

Therefore. How can i reuse the public properties from my ViewModel for both the MultiSelect and Listview without causing validation to fail when the MultiSelect is submitted? Or, am i binding either the MultiSelect or ListView (or both) incorrectly. Note that the Listview is not within a form and is not submitted to the controller.

Thanks for taking the time to have a look at this for me.

Anthony

3 Answers, 1 is accepted

Sort by
0
Ant
Top achievements
Rank 1
answered on 20 May 2014, 08:06 AM
Here is the ModelState.IsValid error message when using a SelectListItem in the MultiSelect

""The parameter conversion from type 'System.String' to type 'System.Web.Mvc.SelectListItem' failed because no type converter can convert between these types."
0
Petur Subev
Telerik team
answered on 21 May 2014, 12:37 PM
Hello Ant,

What you shared looks correct, the only code that bothers me is the get and set methods with the C# logic. Could you replace them with regular get and set methods and see if it makes a difference?

I saw this post:

http://forums.asp.net/t/1715908.aspx?Message+The+parameter+conversion+from+type+System+String+to+type+System+Web+Mvc+SelectListItem+failed+because+no+type+converter+can+convert+between+these+types+

Still it is not clear what might be the case. If still struggling put this in a small demo so we can search for the root of the exception.

Kind Regards,
Petur Subev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Ant
Top achievements
Rank 1
answered on 31 May 2014, 10:52 AM
Hi, I never managed to get the ListView and DropDownList to share the same properties in the end successfully. Either one wouldn't display correctly or the ModelState was invalid. But i did manage to get it working using two different properties derived from the same list. I'm happy with this so thanks for your help.
Tags
General Discussions
Asked by
Ant
Top achievements
Rank 1
Answers by
Ant
Top achievements
Rank 1
Petur Subev
Telerik team
Share this question
or