Partial View with Layout in MVC

Partial View with Layout in MVC


We can use partial view with layout page. Here is the sample code...


Model


public class Menu
{
 public string MenuText { get; set; }
 public string MenuUrl { get; set; }
 public string ToolTip { get; set; }
}

Controller

I have created a common controller and HeaderMenu action  to use server side code.


public ActionResult HeaderMenu()
{
 List<Menu> menu = new List<Menu>
 {
  new Menu(){ MenuText = "About", MenuUrl = "/Home/About", ToolTip = "About us"},
  new Menu(){ MenuText = "Contact", MenuUrl = "/Home/Contact", ToolTip= "Contact us"},
  new Menu(){ MenuText = "Products", MenuUrl = "/Products", ToolTip= "Product Catalog"}
 };

 return PartialView(menu);
}

View 

To use "HeaderMenu" partial view in Shared folder in Views. Change the Html code as per your design.


<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav"> 
                @if (Model != null)
                {
                    foreach (var item in Model)
                    {
                        <li><a href="@item.MenuUrl" title="@item.ToolTip">@item.MenuText</a></li>
                    }
                }
            </ul>
        </div>
    </div>
</div>

Layout page

Finally I called partial view in layout page as below


<body>
     @Html.Action("HeaderMenu", "Common")
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div> 
</body>

Output



Hope this will help you and save your time.

Enjoy !!!

:)

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.


I got below error while calling web api on https and found the solution.

Error


System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

  

Solution

I solved this error by adding this line before calling the web method:

System.Net.ServicePointManager.ServerCertificateValidationCallback = (senderX, certificate, chain, sslPolicyErrors) => { return true; };


Hope this will help you and save your time.

Enjoy !!!

:)

IQueryable - Value cannot be null. Parameter name: property

Value cannot be null.

Parameter name: property


I got below error while I called orderby with IQueryable,

Error


Value cannot be null.
Parameter name: property

StackTrace:
.... OrderBy[T](IQueryable`1 query, String memberName, String direction) ....

I searched on many sites but did not get solution, then I start trial and run method on code. Finally I found that it was my silly mistake, 

Solution


SQL column name and what you passing in code should be same, its case sensitive

I was passing : countryName
SQL Column name: CountryName


Hope this will help you and save your time.

Enjoy !!!

:)

Create and Publish own nuget package

Create and Publish own nuget package


First create class library project, here I have created "DemoApp" project.


Generate the initial manifest  


Open a command prompt and navigate to the project folder containing DemoApp.csproj file.

Run the following command: 

nuget spec DemoApp.csproj

By specifying a project, NuGet creates a manifest that matches the name of the project, in this case DemoApp.nuspec. It also include replacement tokens in the manifest.

Open DemoApp.nuspec in a text editor to examine its contents and udpate



Create Package


From a command prompt in the folder containing your .nuspec file, run the command 

nuget pack

NuGet generates a .nupkg file in the form of identifier-version.nupkg, which you'll find in the current folder.



Publish with nuget push


nuget push DemoApp.1.0.0.nupkg APIKey-GetFrom-Nuget-Account -Source https://api.nuget.org/v3/index.json




Hope this will help you and save your time.

Enjoy !!!

:)

HTTP Error 500.19 - Internal Server Error

HTTP Error 500.19 - Internal Server Error




HTTP Error 500.19 - Internal Server Error

The requested page cannot be accessed because the related configuration data for the page is invalid.


Solution:

Please check if there is any url rewriting tag is there in web.config file, if exists then remove it.


Hope this will help you and save your time.

Enjoy !!!

:)

Get Time Zone list in C#

Get Time Zone list in C#


Here, it is a sample code to get time zone list in C#


string listTimeZone="";

foreach (TimeZoneInfo tzi in TimeZoneInfo.GetSystemTimeZones())
 listTimeZone += tzi.Id + " - " + tzi.DisplayName;




protected void Page_Load(object sender, EventArgs e)
        {
            var inputTime = Convert.ToDateTime("01/15/2019 04:30:00").ToUniversalTime();
 
            TimeZone localZone = TimeZone.CurrentTimeZone;

            lblMessage.Text = localZone.StandardName;

            DateTime outPutTime = inputTime.ToTimeZoneTime(localZone.StandardName);

            lblMessage.Text += "<br>" + inputTime.ToString();
            lblMessage.Text += "<br>India = " + outPutTime.ToString();


            lblMessage.Text += "<br>US Eastern Standard Time = " + inputTime.ToTimeZoneTime("US Eastern Standard Time").ToString();

        }



public static class genericDate
    {
        public static DateTime ToTimeZoneTime(this DateTime time, string timeZoneId = "Pacific Standard Time")
        {
            TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
            return TimeZoneInfo.ConvertTimeFromUtc(time, tzi); 
        } 
    }



Hope this will help you and save your time.

Enjoy !!!

:)

Convert Json to Class in C#

Convert Json to Class in C#


Here it is a sample code to convert json data into Class object.


namespace JsonToClass
{
    class Program
    {
        static void Main(string[] args)
        {
            var jsonInput = "{ username:'myName',password:'myPass'}";
 
            var data = JsonConvert.DeserializeObject<demo>(jsonInput);
        }
    }
 
    class demo
    {
        public string username { getset; }
        public string password { getset; }
    }
}

Hope this will help you and save your time.

Enjoy !!!

:)