Skip to content

Asp.net MVC

Narasimha

Asked Aug 4, 2013, 9:04 PM EDT3 answers
Share
LinkedInX
Hi,

I am new to asp.net MVC. In my application I have a requirement to load dropdown control dynamically and also load the data from database.

I have tried to create the dynamic control but I am unable to load the data also at a time.

I have tried viewmodel like multiple models into single view but I cannot do it.

Can someone provide me an example for the same.       


Thanks
Rao.

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    @Html.LabelFor("Category")
    @Html.DropDownListFor(m => m.CategoryId, Model.Categories)

    Can you please post your code?
    0
  • 0

    Hi,
     Below are the 2 models which I have

    public class EmpModel
        {
            MVCEntities Emp = new MVCEntities();
            [Required]
            public int EmployeeNumber { get; set; }
            [DataType(DataType.Password)]
            [Required]
            public string password { get; set; }
            public string EmployeeName { get; set; }
            public string gender { get; set; }
            public string curproject { get; set; }
            public string Projectcode { get; set; }
            public string curprojectmanager { get; set; }
            public string Designation { get; set; }
            public Boolean manager { get; set; }
            public int man_per { get; set; }
            public string pop3_mailid { get; set; }
            public string levelc { get; set; }
            public Boolean IsActive { get; set; }
    }


    public class ComponentsModel
        {
            public string DisplayText { get; set; }
            public string DisplayComponent { get; set; }
           
        }


    What I want is, after fetching the data from ComponentsModel, I need to create the components dynamically using that model. Like, this is the data in my tbl_components table
     [DisplayText]   [DisplayComponent]
     Employee_Name  textbox
     Employee_Number  textbox
     gender   dropdownlist
     Designation  textbox
     Projectcode  dropdownlist
     Status   checkbox
     mailID   textbox
     manager   dropdownlist

    Means, if my DisplayComponent is textbox, I need to create a textbox. If my Display Component is DropDownList, I need to create a Dropdown. And want to load or bind data in these dropdowns using tbl_employee table.
    So, for this what I did is, I have created a ViewModel by combining these 2 models as below

    public class MainView
        {
                public ComponentsModel Comps { get; set; }
                public IEnumerable EmpList { get; set; } // since I need the list of say project maangers to populate a particular dropdown.
        }


    ----------------Below is my Controller, which I have used to create a final view

    public ViewResult MainViewCtrl()
            {
                MainView mainModel = new MainView();

                mainModel.Comps = new ComponentsModel();
                mainModel.EmpList = new List
                {
      getddlvalues()

                }.AsEnumerable();
                return View(mainModel);

            }

     public List getddlvalues()
            {
                using (SqlConnection con = new SqlConnection(Connectionclass.connectionstring))
                {
                    List ddlvalues = new List();
                    //string str = string.Empty;
                    SqlCommand cmd = new SqlCommand("Select distinct Loginname from Login", con);
                    con.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        Manageusersmodel ddl1 = new Manageusersmodel();
                        //ddlvalues.Add("Select");

                        ddl1.loginname = dr[0].ToString();
                        ddlvalues.Add(ddl1);
                    }
                    dr.Close();
                    con.Close();
                    return ddlvalues;
                  
                }

            }
    --------------------------------------------------------------------------------------
    And here id the view that have been created using the ViewModel

    @model MVCEmp.Models.MainView




       


           
               
               
               
           

                   
                    @Html.DisplayNameFor(model => model.Comps.DisplayText)
               

                    @Html.DisplayNameFor(model => model.Comps.DisplayComponent)
               

            @foreach (var item in Model)
            {
           
               
                   
                        @Html.DisplayFor(modelItem => item.Comps.DisplayText)
                   
                   
                        @Html.DisplayFor(modelItem => item.Comps.DisplayComponent)
                   
                   
               
            }

       


    --------------------------------------------------------------------------------
    The main issue here is, the View is not allowing to refer to the EmpModel list to populate the dropdown. Even though I have created a combined view called MainView, I'm unable to fetch the components and the list of records to populate the dropdowns at a time.

    So, can anybody provide a solution for this?

    0
  • 0
    Hello Murali,

      Yes, I have posted the code in my previous posts.

    Below is my View

    @model MVCEmp.Models.MainView




       


           
               
               
               
           

                   
                    @Html.DisplayNameFor(model => model.Comps.DisplayText)
               

                    @Html.DisplayNameFor(model => model.Comps.DisplayComponent)
               

            @foreach (var item in Model)
            {
           
               
                   
                        @Html.DisplayFor(modelItem => item.Comps.DisplayText)
                   
                   
                        @Html.DisplayFor(modelItem => item.Comps.DisplayComponent)
                   
                   
               
            }

    0