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 !!!

:)