Why and Why not Asp.net 3.5 MVC Projects ? Asp.net 3.5 MVC Project series – IInd

Hello Friends,

Today we just try to make a sample project so I am taking here task to make a simple friend List in that list we can add friends and view friends.

So just follow the step as I mention below and we together make it easy

Step 1:- Add A project for Asp.net MVC as shown in below fig.

mvc1

Step 2:- once you done with this you will find default folders of Views, Model and controller.

The point to remember here is all the presentation layer comes in views folder and if there is sub folder in views so there must be a controller with that particular name.

As you see there is home folder in Views so in controller folder there must be Home controller exists.

The view name or we can say our page name will be the function or procedure in controller.

Suppose there is page in views –> Product –> ProductList.aspx

So in our project there must be a product controller and in that controller there must be a sub routine for product List.

There is also a shared folder in our application which is mainly used to keep shared views like Master Page & other custom controls. 

mvc2

Step 3:- Define views in home folder

1) Index.aspx :- it will be our default page. This page having default text and pictures of you

mvc3

2) AddFriend.aspx :- It is used to add friends in our database.

mvc4

3)ViewFriends.aspx:- it is used to search friend by location.

mvc5

3)SearchFriendsByArea.aspx:- it is used to find location of friend on Google map.

mvc6

 Step 4:- Now we create a database for it. In our database there is single table which is “TblFriends” with field lngId, strFirstName, strLastName, strEmailAddress, strAddress, strCity, strCountry, strphone,strBrief intro.

 Step 5:-Now and LINQ to SQL class which is  “FriendsDb.dbml”  in Model. This is use to intract with Database.

 Step 6:- We have Home Controller now we have to add different method in according to our View, Like Index, AddFriend, viewFriends, SearchFriendByArea

Once you done with this. Now we think about how the data will save from add friend.aspx. For this we just mention another function in home Controller which is Save Friend.

Save Method have all the parameter which is on the form like, strFirstName , strLastName etc.

Once you got all the require parameter we just going for add as shown below.

 Function SaveFriend(ByVal FirstName As String, ByVal LastName As String, ByVal Mobile As String, ByVal Email As String, ByVal Address As String, ByVal city As String, ByVal country As String, ByVal BriefIntro As String) As ActionResult

        ‘Require validation…

        ‘ Basic parameter validation

        Dim blnValidate As Boolean = True

        Dim errors As New List(Of String)

        If FirstName.Trim = String.Empty Then

            errors.Add(“Your first name is required!”)

            blnValidate = False

        End If

        If LastName.Trim = String.Empty Then

            errors.Add(“Your Last  name is required!”)

        End If

        If city.Trim = String.Empty Then

            errors.Add(“your city is required!”)

        End If

        If country.Trim = String.Empty Then

            errors.Add(“Country is required!”)

        End If

        If Address.Trim = String.Empty Then

            errors.Add(“Address is required!”)

        End If

        ‘Save Data i

        If blnValidate = True Then

            Dim clsFriend As New MyFriend

            clsFriend.strFirstName = FirstName

            clsFriend.strLastName = LastName

            If Mobile.Trim <> String.Empty Then

                clsFriend.strMobile = Mobile

            Else

                clsFriend.strMobile = Nothing

            End If

            If Email.Trim <> String.Empty Then

                clsFriend.strEmail = Email

            Else

                clsFriend.strEmail = Nothing

            End If

            If country.Trim <> String.Empty Then

                clsFriend.strCountry = country

            Else

                clsFriend.strCountry = Nothing

            End If

            If city.Trim <> String.Empty Then

                clsFriend.strCity = city

            Else

                clsFriend.strCity = Nothing

            End If

            If BriefIntro.Trim <> String.Empty Then

                clsFriend.strBriefIntro = BriefIntro

            Else

                clsFriend.strBriefIntro = Nothing

            End If

            If Address.Trim <> String.Empty Then

                clsFriend.strAddress = Address

            Else

                clsFriend.strAddress = Nothing

            End If

             db.MyFriends.InsertOnSubmit(clsFriend)

            db.SubmitChanges()

             ViewData(“Title”) = “Record is Saved”

            Return RedirectToAction(“ViewFriends”)

        Else

            ViewData(“Errors”) = errors

            Return View(“AddFriend”)

        End If

     End Function ‘SaveFriend

Step 7:- for searching we will do following thing

 Function SearchFriendsByArea(ByVal Area As String, ByVal city As String, ByVal country As String)

        ‘Search Friends And return

        Dim mylist = From f In db.MyFriends Where f.strAddress Like Area Or f.strCity Like city Or f.strCountry Like country Select f Order By f.strFirstName, f.strLastName

        Return View(mylist.ToList)

    End Function ‘SearchFriendsByArea

Here the things to understand the Return view Parameter. The Return view parameter is again a data collection which we will provide to search Friend’s View.

On this page we have to consider only one thing which is as follows

Inherits System.Web.Mvc.ViewPage(Of List(Of MyFriend))

 Means we provide View Data a data list of Linq Class which is MyFriends.

Now we can use it in aspx page.

<%  For Each mfriend As MyAddressBook.MyFriend In ViewData.Model%>

            <tr>

                <td>

                    <% =mfriend.strFirstName%>

                </td>

                <td>

                    <% If mfriend.strLastName Is Nothing = False AndAlso mfriend.strLastName.Trim <> String.Empty Then%>

                    <%=mfriend.strLastName%>

                    <%Else%>

                    &nbsp;

                    <% End If%>

                </td>

                <td>

                    <% If mfriend.strAddress Is Nothing = False AndAlso mfriend.strAddress.Trim <> String.Empty Then%>

                    <%=mfriend.strAddress%>

                    <% Else%>

                    &nbsp;

                    <% End If%>

                </td>

                <td>

                    <%=mfriend.strCity%>

                </td>

                <td>

                    <%=mfriend.strCountry%>

                </td>

                <td>

                    <% If mfriend.strEmail Is Nothing = False AndAlso mfriend.strEmail.Trim <> String.Empty Then%>

                    <%=mfriend.strEmail%>

                    <%Else%>

                    &nbsp;

                    <% End If%>

                </td>

            </tr>

            <% Next%>

 So in this way we just add records and view data.

You can download source Code from http://indiandotnet.tk

Thanks

Enjoy programming 🙂

Keep doing great Job 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s