Click here to Skip to main content
15,064,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
I am working in asp.mvc currently and am developing employee module.i got strucked with dynamically created controls problem is I have a master class like below
public  class EmployeeSuperClass
    {
        public EmployeeMainTable mainTbl { get; set; }
        public EmployeeEducationDetailTable educationTbl { get; set; }
        public EmployeeAddressTable addressTbl { get; set; }
        public EmployeePreviousCompanyDetailTable previousCompanyDetailTbl { get; set; }
        public EmployeeAssetsTable assetTbl { get; set; }
        public EmployeeFamilyTable familyTbl { get; set; }
        public BindingDropdownlistData ddl { get; set; }
    }

and am going to extend employeefamily table details because this is list to add many family numbers

the family table is
public  class EmployeeFamilyTable
            {


       public EmployeeFamilyTable()
            {
                familyMembersList = new List<EmployeeFamilyTable>();
            }
               //here am declaring some family related fields
//the list is to hold collection of values
            public List<EmployeeFamilyTable> familyMembersList { get; set; }


}

in my controller am passing model to view like this
 [HttpGet]
        public ActionResult CreateEmployeeFamilyDetails(EmployeeSuperClass employeeSuperClass,int i=0)
        {
            super = employeeSuperClass;
            super.familyTbl = new EmployeeFamilyTable();
            super.familyTbl.familyMembersList.Insert(0,new FamilyFields());
                        return View("CreateEmployeeFamilyDetails", super);
        }

the final one is create family members view that should be look like this
     @for (var j = 0; j < Model.familyTbl.familyMembersList.Count; j++)
                {
                    <tr>
                        <th><span class="rownumber"></span></th>
                        <td>
                    <div class="editor-field">

                        @Html.TextBoxFor(model => model.familyTbl.familyMembersList[j].EmployeeFamilyPersonName)
                        @Html.ValidationMessageFor(model => model.familyTbl.familyMembersList[j].EmployeeFamilyPersonName)

//More fields

actual problem is when I post this form its only taking first count and if I want to add more member to this list using jquery am creating new row with dynamic controls for same field

and am using some class to handle modelbinding according to some tutorial that class should be like this
public class ListModelBinder<T> : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var form = controllerContext.HttpContext.Request.Form;

            List<T> result = new List<T>();

            Regex re = new Regex(string.Format(@"^{0}\[(\d+)]\.*", bindingContext.ModelName), RegexOptions.IgnoreCase | RegexOptions.Compiled);

            var candidates = form.AllKeys.Where(x => re.IsMatch(x));
            var indices = candidates.Select(x => int.Parse(re.Match(x).Groups[1].Value)).Distinct();

            var PropInfo = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

            foreach (int i in indices)
            {
                T s = Activator.CreateInstance<T>();
                foreach (var prop in PropInfo)
                {
                    var value = form[string.Format("{0}[{1}].{2}", bindingContext.ModelName, i, prop.Name)];

                    if (s.GetType().GetProperty(prop.Name).PropertyType == typeof(int) || s.GetType().GetProperty(prop.Name).PropertyType == typeof(int?))
                    {
                        if (value != string.Empty)
                        {
                            s.GetType().GetProperty(prop.Name).SetValue(s, Convert.ToInt32(value), null);
                        }
                    }
                    else
                        if (s.GetType().GetProperty(prop.Name).PropertyType == typeof(DateTime) || s.GetType().GetProperty(prop.Name).PropertyType==typeof(DateTime?))
                        {
                            if (value != string.Empty)
                            {
                                s.GetType().GetProperty(prop.Name).SetValue(s, Convert.ToDateTime(value), null);
                            }
                        }
                        else
                        {
                            s.GetType().GetProperty(prop.Name).SetValue(s, value, null);
                        }
                }
                result.Add(s);
            }

            return result;
        }
    }

in my global.aspx am calling this listmodelbinderclass like this
 ModelBinders.Binders.Add(typeof(List<Employee.DAL.ModelClasses.EmployeeFamilyTable>), new ListModelBinder<EmployeeFamilyTable>());

somewhere I missed something don't know exactly please help
Posted
Comments
Nathan Minier 13-Nov-14 8:03am
   
I think I went cross-eyed trying to figure out your data access scheme. Is there a compelling reason that you're passing that SuperClass around everywhere? It looks like it wants to be a DbContext, kind of.

Are you working with a document store rather than a relational database?
Member 10205941 15-Nov-14 2:36am
   
Hi Nathan minier ,am using different layer to access data,,my intesion is to using these model I have to perform inserting operation and finally am sending all model data to data layer here we no need to think of dbContext am using super class to store all data please analyse once

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900