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

Client-side validation in MVC 3 Editor Template

4 Answers 127 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
silviu
Top achievements
Rank 1
silviu asked on 17 Oct 2011, 11:57 AM
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

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)

4 Answers, 1 is accepted

Sort by
0
sta
Top achievements
Rank 1
answered on 25 May 2012, 08:34 PM
Hi Silviu

Have you ever had your problem solved? 
I have precisely the same problem you describing - no client side validation in razor with [required] attribute:

ViewModel:
public class ProfileItem
  {
.....
    [Required]
    public string SelectedLabelId { get; set; }
  }

View (code below is Editor Template called by @EditorFor(<my view model items list>)):
@(Html.Telerik()
     .ComboBoxFor(model => model.SelectedLabelId)
     .BindTo((SelectList)ViewData[typeName])
     .Placeholder("Label " + typeName)
     .SelectedIndex(0))
@Html.ValidationMessageFor(model => model.SelectedLabelId)

ComboBox is working alright, but validation piece just doesn't work! 
Somebody in Telerik, please help!

Thanks.
0
sta
Top achievements
Rank 1
answered on 28 May 2012, 09:33 PM
Wonder why Telerik ignoring this question?! After searching for a few days now, I see many people have the same issue and I see no solution for it by telerik or by anybody else.
I am thinking of dumping Telerik controls altogether and switching to something more predictable, less buggy and better supported. Support is the main part of your controls, guys, not controls themselves. In other words, If you don't support it, no one will ever use it.
Hope this makes sense to you.
0
Matt
Top achievements
Rank 1
answered on 03 Jul 2012, 02:19 PM
This seems to be a problem in the Kendo toolkit as well. I've posted a support request and let's see where it goes from there but I must say I'm pretty frustrated about this. I'm currently evaluating Kendo; I know the server wrappers are in beta but this problem has also been present in the standard MVC extensions since day dot.

Client validation for Editor Templates seems like a fairly basic requirement and it just doesn't work. Can't believe more people haven't come across this - they must all be binding to simple forms.
0
om
Top achievements
Rank 1
answered on 23 Jul 2012, 12:06 PM

@(Html.Telerik().ComboBoxFor(c => c.EmployeeID)

.Name(

"EmployeeID")

 

.InputHtmlAttributes(

new { name = "EmployeeID" })

<input name="EmployeeID-input" ....
I have changed name property by using inputHtmlAttributes, same as viewmodle property name.
It worked for me.

 

Tags
ComboBox
Asked by
silviu
Top achievements
Rank 1
Answers by
sta
Top achievements
Rank 1
Matt
Top achievements
Rank 1
om
Top achievements
Rank 1
Share this question
or