This question is locked. New answers and comments are not allowed.
Hi,
I have 2 questions regarding combo box:
1) I have a View (Create.cshtml) which receives a view model, let's call it ComplexViewModel. From this view I call the editor template view for a member of the ComplexViewModel, which is another view model, let's call it: SimpleViewModel
The editor template view in ~/Shared/EditorTemplates:
The problem is that client-side validation for the combobox in the Editor Template view does not work. Why not?
2) How do I set the selected value of the ComboBox to an item that is not part of the list it is binded to:
Example from question 1). Let's say that "Model.Lists" (which is an IEnumerable<string>) contains the following values: A, B, C. But when the the combobox is rendered by the editor template I want it to have the the selected value set to "Default", which is not in the list it is binded to. It is interesting that I tried setting the value with something outside the list in the "Create.cshtml" view like this:
@Html.Telerik().ComboBoxFor(x => x.Project).BindTo((IEnumerable<SelectListItem>)new SelectList(Model.Lists as System.Collections.IEnumerable, "Default"))
This works in the "Create.cshtml" view, but the exact same line of code does not work when put in the EditorTemplate view (the one mentioned at 1)
I have 2 questions regarding combo box:
1) I have a View (Create.cshtml) which receives a view model, let's call it ComplexViewModel. From this view I call the editor template view for a member of the ComplexViewModel, which is another view model, let's call it: SimpleViewModel
class
ComplexViewModel
{
//...
public
List<SimpleViewModel> TheSimpleViewModel {
get
;
set
;}
//...
}
class
SimpleViewModel
{
//...
public
string
Value {
get
;
set
;}
public
IEnumerable<
string
> Lists {
get
;
set
;}
//...
}
Create.cshtml view:
@model ComplexViewModel
//...
<script src=
'@Url.Content("~/Scripts/jquery.validate.min.js")'
type=
"text/javascript"
></script>
<script src=
"@Url.Content("
~/Scripts/jquery.validate.unobtrusive.min.js
")"
type=
"text/javascript"
></script>
//...
@Html.BeginForm(....)
{
//....
//call the editor template which renders the view for each item in "TheSimpleViewModel" list automatically
@Html.EditorFor(model=>model.TheSimpleViewModel)
//...
}
The editor template view in ~/Shared/EditorTemplates:
@model SimpleViewModel
//...
@Html.Telerik().ComboBoxFor(model => model.Value).BindTo((IEnumerable<SelectListItem>)
new
SelectList(Model.Lists
as
System.Collections.IEnumerable))
@Html.ValidationMessageFor(model => model.Value)
The problem is that client-side validation for the combobox in the Editor Template view does not work. Why not?
2) How do I set the selected value of the ComboBox to an item that is not part of the list it is binded to:
Example from question 1). Let's say that "Model.Lists" (which is an IEnumerable<string>) contains the following values: A, B, C. But when the the combobox is rendered by the editor template I want it to have the the selected value set to "Default", which is not in the list it is binded to. It is interesting that I tried setting the value with something outside the list in the "Create.cshtml" view like this:
@Html.Telerik().ComboBoxFor(x => x.Project).BindTo((IEnumerable<SelectListItem>)new SelectList(Model.Lists as System.Collections.IEnumerable, "Default"))
This works in the "Create.cshtml" view, but the exact same line of code does not work when put in the EditorTemplate view (the one mentioned at 1)