1

I have a strongly typed view, which hold controls(input boxes) to represent a collection item. So for an example , take case of a view for adding an Employee detail and within that there are variable set of input fields to enter Department name. These input fields are to be added dynamically at client side.

Here is the class structure of these two entities:

public class Employee
{

public int EmployeeID{get;set;}
public string Name {get;set; }
public IList<Department> DepartmentList{get;set;}


}


public class Deparment { 
[Required(ErrorMessage="This is a required Field")]
public string Name {get;set; }
public int ID { get;set; }

}

Inputs for department names are generated dynamically and names are set in a way to achieve model binding after posting

<input type='text' class='input-choice' id='txtChoice0' name='Department[0].Name' />

Now my question is how should I apply validation to this ?. Microsoft Validation won't push the validation inside the mvcClientValidationMetadata , reason for this I assume is that framework doesn't see any model binding happening at the time of view load.

Any ideas ??

Kunal
  • 1,913
  • 6
  • 29
  • 45
  • What do you want to validate more precisely? Also are you talking about server side or client side validation or you need both? – Darin Dimitrov May 03 '12 at 22:11
  • And could you include the Attribute tags for the properties in the classes...i.e,[Required(ErrorMessage = "X is required.")] – MikeTWebb May 03 '12 at 22:18
  • @MikeTWebb:thats already there – Kunal May 03 '12 at 23:08
  • @DarinDimitrov: I want to validate on client side, but using Data Annotation. I have all attributes in place, they work for Employee name but question is how to set validation for a collection field like department field. <%:Html.ValidationMessageFor(???) , what will go inside this ? – Kunal May 03 '12 at 23:10

1 Answers1

1

I believe what you are asking for is how to validate values from the dropdownlist with 'Required' attribute. You will need to make some changes to the Employee model.

First of all you will need a 'DepartmentCode' property coz you will be storing the selected Department code from the dropdown.

Then you can have the DepartmentList as IEnumerable<SelectListItem>

so your Employee model will look like

public class Employee
{    
    public int EmployeeID{get;set;}
    public string Name {get;set; }
    [Required(ErrorMessage = "Please select a department")]
    public string DepartmentCode { get; set; }
    public IEnumerable<SelectListItem> DepartmentList{get;set;
}

you can get the DepartmentList like this

public IEnumerable<SelectListItem> DepartmentList 
{
    get
    {
        //Your code to return the departmentlist as a SelectedListItem collection
        return Department
            .GetAllDepartments()
            .Select(department => new SelectListItem 
            { 
                Text = department.Name, 
                Value = department.ID.ToString() 
            })
            .ToList();
    }
}

finally in the view

<%: Html.DropDownListFor(model => model.DepartmentCode, Model.DepartmentList, "select")%>
<%: Html.ValidationMessageFor(model => model.DepartmentCode)%>

Now when you try to submit without selecting a department it should be validated

Prashanth Thurairatnam
  • 4,353
  • 2
  • 14
  • 17
  • Thanks for answering but dont think this helps, I dont need any department code, All I need is a List of department object attached to an employee object. On the UI side, as I said there will be a form for entering employee details and max five textboxes for entering department name. Department is not autopopulated from anywhere. Also this employee department is just an example. In my case both the child and parent objects gets added at same time much like a Survey question and its answers – Kunal May 03 '12 at 23:20
  • OK. So basically based on your example you will have 5 department fields, however it should be validated such that at least one department has a value. I don't think you will be able to achieve this directly by just putting a Required attribute to the collection. You will have to look into the option of custom validation. This discussion may help you (it is MVC3 but should be similar to MVC2). http://stackoverflow.com/questions/5816313/mvc3-unobtrusive-validation-group-of-inputs/5817958#5817958 – Prashanth Thurairatnam May 03 '12 at 23:38
  • Close but not close enough, Form will have a button call "Add Department". This will allow a user to add upto max 5 department. Once you add, validation should work for all of them , whether you just add 1 or 2 ,3,4,5 – Kunal May 03 '12 at 23:42
  • OK here is a blog post by Steven Sanderson. This may not be the exact actual scenario as yours; but I believe you should be able to adopt this. http://blog.stevensanderson.com/2010/01/28/validating-a-variable-length-list-aspnet-mvc-2-style/ – Prashanth Thurairatnam May 04 '12 at 00:31
  • This is for dynamic controls. For now I am struggling to make this work for a list of input boxes. – Kunal May 04 '12 at 15:20