Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,


I am new to MVC, and My requirement is I need to make a facility of adding fields to view. So for that I made a simple viewmodel, and put the fields which I want to render. but I dont know how to add a property to a class dynamically. If that happens then I will simply integrate that too.

e.g. I have a contact us form with fields like firstname, lastname etc. and i need to provide a facility to admin that system should allow them to add a new field for contact us form like middlename,

C#
public class ContactInput 
    {

        [Required]
        [StringLength(100)]
        [Display(Order = 1)]
        public virtual string Name { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        [StringLength(254)]
        [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")]
        [Display(Order = 2)]
        public virtual string Email { get; set; }

        [Required]
        [StringLength(254)]
        [Display(Order = 3)]
        public virtual string Subject { get; set; }

        [Required]
        [StringLength(2000)]
        [DataType(DataType.MultilineText)]
        [Display(Order = 4)]
        public virtual string Message { get; set; }

        [Required]
        [StringLength(2000)]
        [DataType(DataType.MultilineText)]
        [Display(Order = 5)]
        public virtual string Comments { get; set; }

    }
Posted
Comments
Govindaraj Rangaraj 1-Oct-13 9:24am    
How about adding all the required fields into the model class and in the view hide them based on a flag (create flag for each property). When the admin set the flag for this field make it visible in the view.
amolpatil2243 1-Oct-13 9:46am    
If Property addition to class will be possible then that could also be done in that way. I think so, I am not sure.
Govindaraj Rangaraj 1-Oct-13 9:58am    
I meant you dont need to add property dynamically, add them statically along with flags and hide them at runtime based on the flag.
amolpatil2243 1-Oct-13 11:00am    
fields are not defined, Need to add dynamically. implementing CMS

1 solution

You can use ExpandOObject under System.Reflection namespace.

A short example is

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection.Emit;
using System.Dynamic;

namespace Home
{
    class Program
    {
        static void Main(string[] args)
        {
            DynamicClass dc = new DynamicClass();
            dynamic dynObj = dc.MethodA();
            Console.WriteLine(dynObj.Name); //Using your dynamic properties.
            Console.ReadLine();


        }
    }
    class DynamicClass
    {
        public dynamic MethodA()
        {
            dynamic dynamicObj = new ExpandoObject();
            dynamicObj.Name = "Hi"; //Your own property, you can have any number of properties similar to this. 

            
            return dynamicObj;
        }
    }
}
 
Share this answer
 

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