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
Cars and SelectedCarsValues are populated from a db context.
On one view i have my MultiSelect
And on another view I have my ListView
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:
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
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