Wednesday 23 January 2019

Get multiple list using single object

MVC transfer data from controller to list in the form of list. If a page needs to load multiple lists using razor then you will have to wrap multiple lists into single object list. Following code will help you to accomplish this.

Modal


public class MarketingStaffModal
   {
      public string  ID  {get;set;}
       public string Name {get;set;}
      

   }

public class AddTypeModal
    {
        public string ID { get; set; }
        public string AddTypeName { get; set; }
    }
public class AddInvoiceModal
    {
        public List<AddTypeModal> AddType { get; set; }
        public List<MarketingStaffModal> Staff { get; set; }
    }

Controller


public ActionResult AddInvoice()
{
            UserRepository ob = new UserRepository();
            AddInvoiceModal data = new AddInvoiceModal();
            data.AddType = new List<AddTypeModal>();
            data.AddType = ob.GetAddTypeAll();
            data.Staff = new List<MarketingStaffModal>();
            data.Staff = ob.GetAllMarketingStaffName();
            return View(data);
 }

Repository

Get First item form DB

  public List<AddTypeModal> GetAddTypeAll()
  {
            List<AddTypeModal> Record = new List<AddTypeModal>();
            try
            {
  using(SqlConnection dbcon = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ToString()))
                {
                   using (SqlCommand cmd = new SqlCommand("[dbo].[GetAddTypeMaster]", dbcon))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;                       
                        if (dbcon.State == ConnectionState.Closed)
                            dbcon.Open();
                        using (SqlDataReader drOb= cmd.ExecuteReader())
                        {
                            if (drOb.HasRows)
                            {

                                while (drOb.Read())
                                {
                                    AddTypeModal data = new AddTypeModal();
                                    data.ID = drOb.IsDBNull(0) ? "0" : Convert.ToString(drOb[0]);
                                    data.AddTypeName = drOb.IsDBNull(1) ? "-" : Convert.ToString(drOb[1]);
                                    Record.Add(data);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return Record;
        }

Get Second List from DB

  public List<MarketingStaffModal> GetAllMarketingStaffName()
  {
            List<MarketingStaffModal> Record = new List<MarketingStaffModal>();
            try
            {
                using (SqlConnection dbcon = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ToString()))
                {
                    using (SqlCommand cmd = new SqlCommand("[dbo].[GetMarketingStaffName]", dbcon))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                       

                        if (dbcon.State == ConnectionState.Closed)
                            dbcon.Open();
                        using (SqlDataReader drOb= cmd.ExecuteReader())
                        {
                            if (drOb.HasRows)
                            {

                                while (drOb.Read())
                                {
                                    MarketingStaffModal data = new MarketingStaffModal();
                                    data.ID = drOb.IsDBNull(0) ? "" : Convert.ToString(drOb[0]);
                                    data.Name = drOb.IsDBNull(1) ? "" : Convert.ToString(drOb[1]);
                                   
                                    Record.Add(data);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return Record;
  }

Razor


@model WZMPortal.Models.AddInvoiceModal

<select class="form-control" id="ddlAddType" onblur="BlurRemoveCss('ddlAddType')">
                        <option value="">Select Add Type</option>
                        @foreach (var item in @Model.AddType)
                        {
                            <option value="@item.ID">@item.AddTypeName</option>
                        }
             </select>

<select class="form-control" id="ddlAddType" onblur="BlurRemoveCss('ddlAddType')">
                        <option value="">Select Add Type</option>
                        @foreach (var item in @Model.Staff)
                        {
                            <option value="@item.ID">@item.Name</option>
                        }
                    </select>

No comments:

Post a Comment