MultiSelectFor and ListView Binding

4 posts, 0 answers
  1. Ant
    Ant avatar
    18 posts
    Member since:
    Dec 2013

    Posted 19 May 2014 Link to this post

    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

  2. Ant
    Ant avatar
    18 posts
    Member since:
    Dec 2013

    Posted 20 May 2014 Link to this post

    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."
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 21 May 2014 Link to this post

    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.

     
  5. Ant
    Ant avatar
    18 posts
    Member since:
    Dec 2013

    Posted 31 May 2014 Link to this post

    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.
Back to Top
UI for ASP.NET MVC is VS 2017 Ready