Monday, March 9, 2015

Working with Drop Down list using MVC

File->New->Project



select : ASP.NET MVC 4- Application
AND CLICK ON OK


And select the project template and click on ok




And Go to Controller Folder and Right Click  AddController.




Go to  Controller and Select the Method Index and Right CLick on it.





and Now, go to Index.cshtml


and Write the following Code, ( this is binding Static Data)






Output:







Here, in the above example second parameter is  a collection of SelectListItem  which is used as Datasource for  DropDownList 



2  


public ActionResult Index()

        {
            List<string> ListItems = new List<string>();
            ListItems.Add("Select");
            ListItems.Add("India");
            ListItems.Add("a");
            ListItems.Add("d");
            ListItems.Add("d Africa");
            SelectList Countries = new SelectList(ListItems);
            ViewData["Countries"] = Countries;
            return View();
        }



Index.cshtml




Output:






3

With Model.


Now Go to Solution Explorer




And again go to solution explorer and add new item 
and add ado.net entity frame work




And click on add



Click on New Connection.






Give the Details and Click on Ok Button.






Click on Finish.





Controller.cs




public ActionResult Index()
        {
            RNDDatabaseEntities db = new RNDDatabaseEntities();
            ViewBag.Countries = new SelectList(db.Countries, "CountryID", "Countryname");
            return View();

        }


Index.cshtml


@model IEnumerable<MvcApplication3.Country>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@Html.DropDownList("Countries", "Select")




Output:



4

Controller.cs

Index.cshtml






5





Index.cshtml


@model IEnumerable<MvcApplication3.Models.CountryModel>

@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.DropDownList("SelectedItem", (IEnumerable<SelectListItem>)ViewData["ListItems"])



output:




How to Get Email Groups from Outlook (Different Active Directory Groups)

Design the Form as Following(Default.aspx)

<table>
<tr>
<td>
<asp:ListBox ID="lstGroups"runat="server"Height="520px"width="200px"AutoPostBack="true"EnableViewState="true"
onselectedindexchanged="lstGroups_SelectedIndexChanged"></asp:ListBox>
</td>
<td valign="top">
<asp:ListBox ID="lstGroupmembers"runat="server"Height="300px"Width="200px"AutoPostBack="true"></asp:ListBox>
</td>
</tr>

</table>




Default.aspx.cs

Add the following Namespaces
usingSystem.Collections.Specialized;
This namespace is used for  StringCollection

Add the following reference.
·       System.DirectoryServices.
·       System.DirectoryServices.AccountManagement.

Then u can add the namespace
using System.DirectoryServices;
using System.Configuration;

-----------------------------------------------------------------------------------------------
Write the web.config File as following.
  <appSettings>
    <add key="DomainName" value="ABC0"/>

<addkey="DefaultActiveDirectoryServer"value="LDAP://ABC0"/>
  </appSettings>


Write the following Code in Page Load
protected void Page_Load(object sender, EventArgs e)
    {
        foreach (string @group in GetGroups())
        {
            lstGroups.Items.Add(@group);
        }
    }


Write the following Methods in the Page.
public StringCollection GetGroupMembers(string strDomain, string strGroup)
   
{
        StringCollection groupMemebers = new StringCollection();

        try
        {
            DirectoryEntry ent = new DirectoryEntry("LDAP://DC=" + strDomain + ",DC=com");
            DirectorySearcher srch = new DirectorySearcher("(" + strGroup + ")");
            SearchResultCollection coll = srch.FindAll();
            foreach (SearchResult rs in coll)
            {
                ResultPropertyCollection resultPropColl = rs.Properties;
                foreach (Object memberColl in resultPropColl["member"])
                {
                    DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://" + memberColl);
                    System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties;
                    object obVal = userProps["sAMAccountName"].Value;
                    if (null != obVal)
                    {
                        groupMemebers.Add(obVal.ToString());
                    }
                }
            }
        }

        catch (Exception ex)
        {
            Trace.Write(ex.Message);
        }

        return groupMemebers;

    }


public static List<string> GetGroups()
    {
        DirectoryEntry objADAM = default(DirectoryEntry);
        // Binding object.
        DirectoryEntry objGroupEntry = default(DirectoryEntry);
        // Group Results.
        DirectorySearcher objSearchADAM = default(DirectorySearcher);
        // Search object.
        SearchResultCollection objSearchResults = default(SearchResultCollection);
        // Results collection.
        string strPath = null;
        // Binding path.
        List<string> result = new List<string>();
        string Path = Convert.ToString(ConfigurationManager.AppSettings["DefaultActiveDirectoryServer"]);
        // Construct the binding string.
        strPath = Path;
        //Change to your ADserver

        // Get the AD LDS object.
        try
        {
            objADAM = new DirectoryEntry(strPath);
            objADAM.RefreshCache();
        }
        catch (Exception e)
        {
            throw e;
        }

        // Get search object, specify filter and scope,
        // perform search.
        try
        {
            objSearchADAM = new DirectorySearcher(objADAM);
            objSearchADAM.Filter = "(&(objectClass=group))";
            objSearchADAM.SearchScope = SearchScope.Subtree;
            objSearchResults = objSearchADAM.FindAll();

        }
        catch (Exception e)
        {
            throw e;
        }

        // Enumerate groups
        try
        {
            if (objSearchResults.Count != 0)
            {
                foreach (SearchResult objResult in objSearchResults)
                {
                    objGroupEntry = objResult.GetDirectoryEntry();

                    result.Add(objGroupEntry.Name);
                }
            }
            else
            {
                throw new Exception("No groups found");
            }
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }

        return result;
    }

========================================


protected void lstGroups_SelectedIndexChanged(object sender, EventArgs e)
    {
        string domainName = Convert.ToString(ConfigurationManager.AppSettings["DomainName"]);
        string group = lstGroups.SelectedValue;
        StringCollection groupMembers = this.GetGroupMembers(domainName, group);
        //Response.Write("The members of"+group +"are");
        lstGroupmembers.Items.Clear();
        foreach (string strMember in groupMembers)
        {
            //Response.Write("<br><b>" + strMember + "</b>");
            lstGroupmembers.Items.Add(strMember);
        }
    }



















Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...