Ads

Friday, 5 December 2014

Understanding Model View Controller in Asp.Net MVC

MVC interduced in 1970s.
It actually splits the application to 3 main aspects (Model, View, Controller)

M in MVC: (Model)
Classes that describes the business logic and data, business rules for how the data can be changed and manipulated. It also handles the Data Access Layer. it can access by both view & controller
It can be split in diff layers
  • Data Access Layer : Access and manipulate the database of your application.
  • Business Layer: Implement your business logic and validations for your application.
V in MVC: (View)
Responsible for transforming a model or models into UI. It gives UI for all validation and logic
it may use model to display data in page.
C in MVC: (Controller)
Responsible for controlling the application logic and acts as the coordinator between the View and the Model. It receive input from users via the View, Then process data in model and passing result back to view. It inherits system.mvc.controller

Apart from this, there must have a Global.asax file in root folder, and a web.config like a traditional ASP.NET application.

The specified URL will first get parsed via application_start() method inside Global.asax file. From the requested URL, it will parse the Controller, Action and ID.

So for [xyz.com]/home/index/:
  • Controller = home
  • Action = index()
  • ID = empty — we have not specified ID in [xyz.com]/home/index/, so it will consider as empty string
MVC now finds the home controller class in controller directory. A controller class contains different action methods,
There can be more than one action method, but MVC will only invoke the action method which has been parsed from the URL, its index() in our case.
So something like: homeController.index() will happen inside MVC controller class.

Invoking action method can return plain text string OR rendered HTML by using view.
Invoking view will return view(). A call to view will access the particular ASPX page inside the view directory and generate the rendered HTML from the ASPX and will respond back to the browser.
In our case, controller was home and action was index(). So calling view() will return a rendered HTML from the ASPX page located at /views/home/index.aspx.









Tuesday, 2 December 2014

Filter Column based on another column - Cascade Drop Down JavaScript

We can use Content Editor Web Part to add java script in page (NewForm.aspx)

Lets Create 3 lists.
List-1 called "State" : Custom list with a column "Title" with all states.

List-2 called "CityState" : Custom list with a column 'City' and one for its State. State Column will be lookup to the previous list "State". Here I have renamed "Title" column as "City".



City will hold column "City" information. State will be a dropdown that loads as a lookup.

List called "Data" : Custom list with a column to have Company name, a column for State[ lookup to the column "Title" in first list "State"], a column for city[ lookup to "City" column of second list "CityState"]


Now State is a lookup column and brings all the value from the first list "State".


Now City is a lookup column and brings all the value from the column "City" in the list "CityState" . So far no filtering is happening.



Step 3: Edit the page. You can modify the URL to edit NewForm.aspx. To the end of the URL append PageView=Shared&ToolPaneView=2. This will open the form in edit mode.

your URL must be like this http://sample.com/sites/MySPSite/Lists/Data/NewForm.aspx?PageView=Shared&ToolPaneView=2

Step 4: Add Content Editor Web Part.



Step 5: Write the following code in a text. save as html. upload into SharePoint.Link this html page to content editor web part or Write code in Web Part's text editor directly. SPSErvices JS File  helps us with the SPCascadeDropDown function.


<SCRIPT type=text/javascript src="http://mysite.com/sites/MySite/Documents/jquery-1.4.2.min.js"></SCRIPT>
<SCRIPT type=text/javascript src="http://mysite.com/sites/MySite/Documents/jquery.SPServices-0.5.6.min.js"></SCRIPT>
<SCRIPT type=text/javascript>

$(document).ready(function(){

        $().SPServices.SPCascadeDropdowns({
        relationshipList:"CityState",
        relationshipListParentColumn:"State",
        relationshipListChildColumn:"Title",
        parentColumn:"State",
        childColumn:"City"
      });
  
});


</SCRIPT>

Step 6: After the content editor web part is saved and you exit the page from edit mode, City gets filtered based on the State.




Step 7: To remove the identity that the code was generated from Content Editor Web Part, within the Web Part Setting, Under Appearance, For Chrome Type select "None". Click OK. Click Apply. Publish the Page.

Step 8: Now instead of showing all cities depending upon the selected state, you need to show only a particular set of cities based on a CAML query, then that also can be achieved.

Step 9: Create a column of type "Yes\No" in second list "CityData" called "Visible".




Step 10: Now add the CAML query to the SPServices.SPCascadeDropDown function. Notice the comma after the ChildColumn.
 $(document).ready(function(){

        $().SPServices.SPCascadeDropdowns({
        relationshipList:"CityState",
        relationshipListParentColumn:"State",
        relationshipListChildColumn:"Title",
        parentColumn:"State",
        childColumn:"City",
        CAMLQuery: "<Eq><FieldRef Name='Visible' /><Value Type='Boolean'>1</Value></Eq>"
      
      });
  
});

Step 11:  Now for a selected State, Its relevant Cities with Visible ="True" only gets displayed.









Monday, 1 December 2014

Data Contract and Data Member in WCF - 5

Before knowing this, lets know some of the terms used in WCF.



Serialization: It’s the process of converting .net objects to XML representation format.
De-serialization: The reverse of serialization. i.e to reconstruct the same .net object from XML format.
By default WCF uses DataContractSerializer serialized.
For complex types to be serialized, it can either use Serializable attribute or DataContract attribute.

Eg. of complex type can be Employee, student …….




By default DataContract serializer will serialize all public properties of complex type in alphabetical order. We don’t have to use these attributes explicitly.
Private fields and properties are not serialized by DataContract serializer.

If we see this serialization of items in WSDL document then we must clear about this.




Two ways to serialize complex types:
·         Decorate complex type with [Serializable] attribute
o   Serializes all fields, No granular control of fields.
o   Not have explicit control on what field we exclude and include from serialized data
·         Decorate with [DataContract] attibute (preferred)
o   We have control over which fields we need to include and exclude
o   It only serializes the fields which are marked as [DataMember] attributes
o   It will not serialize which are not decorated with [DataMember] attribute
o   You can define XML namespace for our data
o   You can also define Name, Order , IsRequired inside [DataMember] attribute
o   Also serialize private fields and properties


Here we can see
Name attribute in data member: used to give explicit name to fields.
Order attribute in data member: used to define custom order of fields.

Ads