Thursday, 16 June 2016

Different Ways to Pass Data to Partial View

Partial views are just Views which you can reuse across your ASP.NET MVC application. If you are from ASP.NET Web Forms background, you can think of Partial View as User Control. Partial views can contain anything – HTML elements for displaying the data or getting the input from the users. In this tip, we are going to see the different ways to pass data to partial view for displaying data to the partial view.

1. Pass Data from Enclosing View to Partial View

@{
   ViewBag.Title = "Index";
   double piValue = 3.14;
}

<h2>Index</h2>
@Html.Partial("_MyPartial", piValue)

In the partial view (_MyPartial.cshtml), I can consume the passed variable value by accessing @Model variable.
Data received is: @Model

2. Pass Data to Partial View using ViewBag/ViewData

 in the below Index action method, I am passing the piValue value from action method to the View.


        public ActionResult Index()
        {
            ViewBag.piValue = 3.14;
            return View();
        }

 In the View(Index.cshtml), I am passing the piValue to the partial view. 


@{
   ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.Partial("_MyPartial", (double) @ViewBag.piValue)

3. Pass Data to Partial View using Strongly Typed Model

 I have created instance of this class and pass it to the View. In the real world, we would be getting the value from database. 

    public ActionResult Index()
      {
            //Assume we are getting below data from the database
        var model = new Employee { Name = "John", Location = "New York" };
        return View(model);
      }

we can just use Modelkeyword to pass the data to the Partial View.


@model PassingData.Models.Employee

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@Html.Partial("_MyPartial",Model)

4. Pass Data to Partial View using ViewData Dictionary

@{
    Html.RenderPartial("_CreateEditDisplay", Model, new ViewDataDictionary { { "Submit", true }, { "Action", "Edit" }, { "ReadOnly", false } });
}

Another Way -

@{
    Html.ViewData.Add(new KeyValuePair&lt;string, object&gt;("Submit", true));
    Html.ViewData.Add(new KeyValuePair&lt;string, object&gt;("Action", "Edit"));
    Html.ViewData.Add(new KeyValuePair&lt;string, object&gt;("ReadOnly", false));

    Html.RenderPartial("_CreateEditDisplay", Model, Html.ViewData);
}

Continue Reading →

Tuesday, 10 May 2016

Parallel Programming

Introduction To Parallel Programming
Parallel programming is a programming model wherein the execution flow of the application is broken up into pieces that will be done at the same time (concurrently) by multiple cores, processors, or computers for the sake of better performance. Spreading these pieces across them can reduce the overall time needed to complete the work and/or improve the user's experience. However, this is not always the case as there are several pitfalls you need to be aware of and we will certainly discuss them through the series.

Difference between asynchronous and parallel programming?
  1. When you run something asynchronously it means it is non-blocking, you execute it without waiting for it to complete and carry on with other things.
  2. Parallelism means to run multiple things at the same time, in parallel. Parallelism works well when you can separate tasks into independent pieces of work.
Asynchonous programming solves the problem of waiting around for an expensive operation to complete
before you can do anything else. If you can get other stuff done while you're waiting for the operation to
complete then that's a good thing. Example: keeping a UI running while you go and retrieve more data
from a web service. Parallel programming is related but is more concerned with breaking a large task into smaller chunks that
can be computed at the same time. The results of the smaller chunks can then be combined to produce the
overall result.

Task Class

Task class Represents an asynchronous operation.

A Simple Example is below:

using System.Threading.Tasks;

class Program
    {
     static void Main(string[] args)
        {
            var arrTask = new List<Task>();
            Task taskShowDate = Task.Run(() =>
            {
                ShowDate();
            });
            arrTask.Add(taskShowDate);

            Task taskShowTime = Task.Run(() =>
            {
                ShowTime();
            });
            arrTask.Add(taskShowTime);

            Task.WaitAll(arrTask.ToArray(), 10000000);
            //Parallel.Invoke(ShowDate, ShowTime);
            Console.ReadLine();
        }
        public static void ShowDate()
        {
            Console.WriteLine(DateTime.Now.ToLongDateString());
        }

        public static void ShowTime()
        {
            Console.WriteLine(DateTime.Now.ToLocalTime());
        }
  }

Task.WaitAll(): Waits for all of the provided Task objects to complete execution.

Why we need Tasks

It can be used whenever you want to execute something in parallel. Asynchronous implementation is easy in a task, using’ async’ and ‘await’ keywords.


here is another important link Introducing-task-programming
Continue Reading →

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (47) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (55) CD (1) CI (2) CloudComputing (2) Coding (10) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (20) Entity Framework (5) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) jwtToken (4) Lamda (3) Linq (10) microservice (4) Mongodb (1) MVC (46) NodeJS (8) React (10) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (3) UI (1) UnitTest (2) WCF (14) Web Api (16) Web Service (1) XMl (1)

Dotnet Guru Archives