Html.Partial,Html.RenderPartial,Html.Action and Html.RenderAction in ASP.NET MVC

Html.Partial and Html.RenderPartial     

  The Html.Partial helpers renders a partial view in to string.You can reuse this Partial Views in Multiple Views. Partial has four overloads
public void Partial(string partialViewName);

public void Partial(string partialViewName, object model);

public void Partial(string partialViewName, ViewDataDictionary viewData);
public void Partial(string partialViewName, object model,ViewDataDictionary viewData);
 Example : 
 @Html.Partial("MyPartial")
 
Difference Between Render Partial and Partial

RenderPartial writes directly to the response output stream instead of returning a string. So, you should place RenderPartial inside a code block instead of a code expression

Example :
 @{Html.RenderPartial("MyRenderPartial");}

 So, which should you use, Partial or RenderPartial?

In general, you should prefer Partial to RenderPartial because Partial is more convenient (you don’t have to wrap the call in a code block with curly braces). However, RenderPartial may result in better performance because it writes directly to the response stream.

Html.Action and Html.RenderAction

         Html.Action and Html.RenderAction are similar to Partial and RenderPartial.Action  executes a separate controller action and display the results.the only difference between Action and RenderAction is that RenderAction writes directly to the response.

 Example :
Imagine you are using the following controller
public class ActionDemoController : Controller {
public ActionResult Index() {
return View();
}

[ChildActionOnly]
public ActionResult DisplayMenu() {
var menu = GetMenuFromSomewhere();
return PartialView(menu);
}
}

The Menu action builds a menu model and returns a partial view with just the menu:
@model Menu
<ul>
@foreach (var item in Model.MenuItem) {
<li>@item.Text</li>
}
</ul>
In your Index.cshtml view, you can now call into the Menu action to display the menu:
<html>
<head><title>Index with Menu</title></head>
<body>
@Html.Action("DisplayMenu")
<h1>Welcome to the Index View</h1>
</body>
</html>

Notice that the Menu action is marked with a ChildActionOnlyAttribute. The attribute prevents the runtime from invoking the action directly via a URL. Instead, only a call to Action or RenderAction can invoke a child action. The ChildActionOnlyAttribute isn’t required, but is generally recommended for child actions.
Passing values to Action
       Sometimes you want to pass parameters in to the action.
Example:
[ChildActionOnly]
public ActionResult Menu(MenuParameters param) {
return PartialView(
param);

}

@Html.Action(“Menu”, new {options = new MenuParameters { Width=400, Height=500 } })

Enjoy Coding…

Load Partial View as Html By using jQuery – Ajax in ASP.NET MVC

Introduction

In ASP.NET MVC we can return a Partial View in the form of PartialViewResult using the PartialView method.
The following example shows Partial update scenarios using Ajax.Using jQuery to load the contents of a PartailView in to the current view using an Ajax call. In this example i have load a simple grid using Ajax.
Controller and Action Result

public class PartialController : Controller
{
public ActionResult GetPartialResult()
{
var list = new List<Album>
{
new Album {Title = "Album1"},
new Album {Title = "Album2"},
new Album {Title = "Album3"},
new Album {Title = "Album4"}
};
return PartialView(list);
}

}

public class Album
{
public string Title { get; set; }
}

Right click the ActionResult and Create a PartialView like below
@model IEnumerable<Mvc4Demos.Controllers.Album>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
</tr>
}
</table>

Here when a button click load the PartialViewResult by using Ajax and Append in to a Result div
<script type="text/javascript">
$(document).ready(function () {
$('#btn').click(function () {
$('#result').load('/Partial/GetPartialResult');
});
});

</script>
<input type="button" id="btn" value="PartialExample"/>
<div id="result"></div>

Enjoy Reading…

Call Restful Service using HttpWebRequest and Post data

Introduction

This article helps  you call Restful service using HttpWebRequest and Post Data to a particular service.The below demonstrate a sample restful service in WCF and call as HttpWebRequest.

WCF Restful Service Source Code

Contract

[ServiceContract]
public interface IRestService
{
[WebInvoke(Method = “POST”, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, UriTemplate = “/MyServiceMethod”)]
[OperationContract]
void MyServiceMethod(PostedInformation postedInformations);
}

Implementation

public void MyServiceMethod(PostedInformation postedInformations)
{
//do implemention code here
}

DataContract
[DataContract]
public class PostedInformation
{
[DataMember]
public List<string> To { get; set; }
[DataMember]
public string SenderEmail { get; set; }
[DataMember]
public string Subject { get; set; }
[DataMember]
public string SenderFullName { get; set; }

}

Calling the service using HttpWebRequest

PostedInformation postedInformations = new PostedInformation()
{
SenderEmail = “aaa@bbb.com”,
SenderFullName = “test”,
To = new List<string>() { “ccc@eee.com” }
};

var dataToSend = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(postedInformations ));
//Passyour service url to the create method
var req =
HttpWebRequest.Create(“http://localhost/MyServer/YourServiceName.svc/MyServiceMethod&#8221;);
req.ContentType = “application/json”;
req.ContentLength = dataToSend.Length;
req.Method = “POST”;
req.GetRequestStream().Write(dataToSend, 0, dataToSend.Length);
var response = req.GetResponse();

Enjoy coding…

Give permissions to a folder pro grammatically using .net in windows

The below source codes allow you to give permissions to everyone for folder. When we need to upload or save some documents to a folder sometimes restricts the actions because of some permissions . In order to avoid this issue we can change permissions of a folder programmatically

var dInfo = new DirectoryInfo(@"C:\\MyFolder");
DirectorySecurity dSecurity = dInfo.GetAccessControl();
dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
dInfo.SetAccessControl(dSecurity);

Enjoy coding….

Linked List

Introduction 

In computer science, a linked list is a data structure consisting of a group of nodes which together represent a sequence.

Node

The Node full fills two function. The first function is a mechanism of provide a piece of data to the node.For example integer value 1 0 in the below figure.

images

and the second function is to pointing to the next node.

How to create a Linked List using C#?


public class LinkedList
{

public static void ConstructLinkedList()
{
//Creating a first node and assign the value 1 to the firstnode
var firstNode = new Node {CurrentValue = 1};
var secondNode = new Node {CurrentValue = 2};
//Pointing the FirstNode to the SecondNode
firstNode.NextNode =secondNode;
//Creating the third node and assign a value to 3
var thirdNode = new Node {CurrentValue = 3};
//Pointing the second node to the third node
secondNode.NextNode = thirdNode;
//Display all the Linked List
DisplayLinkedList(firstNode);
}

public static void DisplayLinkedList(Node node)

{
//Looping through all the nodes and print the value in it
while (node!=null)
{
Console.WriteLine(node.CurrentValue);
node = node.NextNode;
}
}
}

// This is a node which contain two properties , One for setting the current node and other is for setting the next node
public class Node
{
public int CurrentValue { get; set; }
public Node NextNode { get; set; }
}

Enjoy coding…..